Skip to content Skip to sidebar Skip to footer

Python Producer Can Send Via Shell, But Not .py

I have a running and tested Kafka cluster, and am trying to use a Python script to send messages to the brokers. This works when I use the Python3 shell and call the producer metho

Solution 1:

As you can see, it returns a future.

Kafka clients will batch records, they don't immeadiately send one record at a time, and to make it do that, you will need to wait or flush the producer buffer so that it'll send before the app exits. In other words, the interactive terminal keeps the producer data in-memory, running in the background, and the other way discards that data

As the docs, show

future = producer.send(...)

try:
    record_metadata = future.get(timeout=10)
except KafkaError:
    # Decide what to do if produce request failed...
    log.exception()
    pass

Or just put producer.flush(), if you don't care about the metadata or grabbing the future.

Post a Comment for "Python Producer Can Send Via Shell, But Not .py"