kafka-python icon indicating copy to clipboard operation
kafka-python copied to clipboard

Add context manager support to Consumers and producers

Open lyytinen opened this issue 8 years ago • 4 comments

Would it make sense for the consumers and producers to implement the context management protocol? So that one could write:

from kafka import KafkaProducer
with KafkaProducer(bootstrap_servers='localhost:1234') as producer:
    for _ in range(100):
        producer.send('foobar', b'some_message_bytes')

lyytinen avatar May 11 '17 18:05 lyytinen

Could the producer be entered again? It's just that with DB's it's common to have a pool of connections and with protocol typically is used to acquire 1 connection. Would adding context support not cause confusion like:

global_producer = KafkaProducer(bootstrap_servers='localhost:1234')

def send_message_handler(self, request):
    with global_producer as conn:
        conn.send("foobar", b"some_message_bytes")

And from this example on, the Producer and Consumer are both long running instances, and only in scripts do you create/destroy it in 1 function. Mostly it would be initialized in startup hook and stopped in teardown or something. At least those were my thoughts about not adding it for aiokafka...

tvoinarovskyi avatar May 11 '17 18:05 tvoinarovskyi

Fair points. It's true that the use cases that would benefit from context support are marginal but it would still be nice to do those with ease. Not entirely sure about the added confusion also because by using "with" you already acknowledge that something is going to happen behind the curtain.

lyytinen avatar May 12 '17 13:05 lyytinen

fwiw, there is an old (unsupported) context manager in kafka.context that was designed to manage consumer offsets w/ SimpleConsumer.

dpkp avatar May 12 '17 15:05 dpkp

Another alternative would be:

import contextlib
from kafka import KafkaProducer

with contextlib.closing(KafkaProducer(bootstrap_servers='localhost:1234')) as producer:
    for _ in range(100):
        producer.send('foobar', b'some_message_bytes')

dpkp avatar Mar 09 '18 20:03 dpkp