Why do examples and docs not show using context managers for the producer and consumer? #925
Answered
by
ods
MicaelJarniac
asked this question in
Q&A
-
On the docs and examples, I've only seen it done like this: producer = aiokafka.AIOKafkaProducer(bootstrap_servers="localhost:9092")
await producer.start()
try:
await producer.send_and_wait("my_topic", b"Super message")
finally:
await producer.stop() But from reading the code and testing, the following also seems to be possible: async with aiokafka.AIOKafkaProducer(bootstrap_servers="localhost:9092") as producer:
await producer.send_and_wait("my_topic", b"Super message") The same goes for the consumer. I wanted to know why is this omitted from the docs and examples. Is there a drawback? Is it not recommended? I personally would prefer using context managers, as it's fewer lines to write, and seems more idiomatic. |
Beta Was this translation helpful? Give feedback.
Answered by
ods
Oct 14, 2023
Replies: 1 comment
-
The |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
MicaelJarniac
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The
async with
approach is usually preferable. It was just added later and documentation hasn't been updated to reflect it.