How to stop Python Kafka Consumer in program?

13,193

Solution 1

Use the iter_timeout parameter to set the waiting time. If set to 10, just like the following piece of code, it will exit if no new message come in in 10 seconds. The default value is None, which means that the consumer will block here even if no new messages come in.

        self.consumer = SimpleConsumer(self.client, "test-group", "test",
                iter_timeout=10)

Update

The above is not a good method. When lots of messages come in, it is hard to set a small enough iter_timeout to guarantee the stopping. So, now, I am using get_message() function, which try to consume one message and stop. None is returned when no new messages.

Solution 2

We can first check the offset of the last message in the topic. Then stop the loop when we have reached that offset.

    client = "localhost:9092"
    consumer = KafkaConsumer(client)
    topic = 'test'
    tp = TopicPartition(topic,0)
    #register to the topic
    consumer.assign([tp])

    # obtain the last offset value
    consumer.seek_to_end(tp)
    lastOffset = consumer.position(tp)

    consumer.seek_to_beginning(tp)        

    for message in consumer:
        print "Offset:", message.offset
        print "Value:", message.message.value
        if message.offset == lastOffset - 1:
            break

Solution 3

Similar solution to Mohit's answer but using the end_offsets function of the consumer.

from kafka import KafkaConsumer, TopicPartition

# settings
client = "localhost:9092"
topic = 'test'

# prepare consumer
tp = TopicPartition(topic,0)
consumer = KafkaConsumer(client)
consumer.assign([tp])
consumer.seek_to_beginning(tp)  

# obtain the last offset value
lastOffset = consumer.end_offsets([tp])[tp]

for message in consumer:
    print "Offset:", message.offset
    print "Value:", message.message.value
    if message.offset == lastOffset - 1:
        break
Share:
13,193
BAE
Author by

BAE

while True: try: learn code think communicate except: vacate

Updated on July 25, 2022

Comments

  • BAE
    BAE almost 2 years

    I am doing Python Kafka consumer (trying to use kafka.consumer.SimpleConsumer or kafka.consumer.simple.SimpleConsumer in http://kafka-python.readthedocs.org/en/latest/apidoc/kafka.consumer.html). When I run the following piece of code, it will run all the time, even if all messages consumed. I hope the consumer will stop if it consume all the messages. How to do it? Also I have no idea how to use stop() function (which is in base class kafka.consumer.base.Consumer).

    UPDATE

    I used signal handler to call consumer.stop(). Some error messages were printed out to the screen. But the program still was stuck in the for-loop. When new messages came in, the consumer consumed them and printed them. I also tried client.close(). But the same result.

    I need some ways to stop the for-loop gracefully.

            client = KafkaClient("localhost:9092")
            consumer = SimpleConsumer(client, "test-group", "test")
    
            consumer.seek(0, 2)# (0,2) and (0,0)
    
            for message in consumer:
                print "Offset:", message.offset
                print "Value:", message.message.value
    

    Any help is welcome. Thanks.

    • Edenshaw
      Edenshaw almost 4 years
      If all messages were consumed, add the consumer_timeout_ms property in the KafkaConsumer constructor, that way, if there are no more messages in the topic, your for loop will end correctly. More Info here: (stackoverflow.com/a/45183265/922457)
  • ryanlee
    ryanlee over 2 years
    this method has one problem is that if the message count is too less, offset will never be committed. because it can not reach the auto commit time interval. And as you break, so it won't commit.