Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/apache-kafka/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Kafka:无限地保持轮询主题_Python_Apache Kafka_Kafka Consumer Api_Kafka Python - Fatal编程技术网

Python Kafka:无限地保持轮询主题

Python Kafka:无限地保持轮询主题,python,apache-kafka,kafka-consumer-api,kafka-python,Python,Apache Kafka,Kafka Consumer Api,Kafka Python,我正在使用python kafka来听一个kafka主题,并使用这些记录。我想让它无止境地进行轮询。下面是我的代码: def test(): consumer = KafkaConsumer('abc', 'localhost:9092', auto_offset_reset='earliest') for msg in consumer: print(msg.value) 这段代码只是读取数据并直接退出。有没有一种方法,即使消息没有被推送到主题上,也能一直听下

我正在使用python kafka来听一个kafka主题,并使用这些记录。我想让它无止境地进行轮询。下面是我的代码:

def test():
    consumer = KafkaConsumer('abc', 'localhost:9092', auto_offset_reset='earliest')
    for msg in consumer:
        print(msg.value)
这段代码只是读取数据并直接退出。有没有一种方法,即使消息没有被推送到主题上,也能一直听下去

任何持续监控主题的相关示例对我来说都是非常好的。

使用


使用


你能告诉我为什么我们需要在这里进行
消费.投票吗?这10是什么意思?@mifol68042超时了:
import time
from confluent_kafka import Consumer


consumer = Consumer({
    'bootstrap.servers': 'localhost:9092',
    'group.id': 'my-consumer-1',
    'auto.offset.reset': 'earliest'
})
consumer.subscribe(['topicName'])

while True:
    try: 
        message = consumer.poll(10.0)

        if not message:
            time.sleep(120) # Sleep for 2 minutes

        if message.error():
            print(f"Consumer error: {message.error()}")
            continue

        print(f"Received message: {message.value().decode('utf-8')}")
    except:
        # Handle any exception here
        ...
    finally:
        consumer.close()
        print("Goodbye")
import time
from kafka import KafkaConsumer

consumer = KafkaConsumer(
     bootstrap_servers=['localhost:9092'],
     auto_offset_reset='earliest',
     group_id='my-consumer-1',
)
consumer.subscribe(['topicName'])

while True:
    try: 
        message = consumer.poll(10.0)

        if not message:
            time.sleep(120) # Sleep for 2 minutes

        if message.error():
            print(f"Consumer error: {message.error()}")
            continue

        print(f"Received message: {message.value().decode('utf-8')}")
    except:
        # Handle any exception here
        ...
    finally:
        consumer.close()
        print("Goodbye")