Matt Howlett has an introduction to Apache Kafka, designed for Python developers:
In the call to the
producemethod, both thekeyandvalueparameters need to be either a byte-like object (in Python 2.x this includes strings), a Unicode object, orNone. In Python 3.x, strings are Unicode and will be converted to a sequence of bytes using the UTF-8 encoding. In Python 2.x, objects of typeunicodewill be encoded using the default encoding. Often, you will want to serialize objects of a particular type before writing them to Kafka. A common pattern for doing this is to subclassProducerand override theproducemethod with one that performs the required serialization.The produce method returns immediately without waiting for confirmation that the message has been successfully produced to Kafka (or otherwise). The
flushmethod blocks until all outstanding produce commands have completed, or the optional timeout (specified as a number of seconds) has been exceeded. You can test to see whether all produce commands have completed by checking the value returned by theflushmethod: if it is greater than zero, there are still produce commands that have yet to complete. Note that you should typically callflushonly at application teardown, not during normal flow of execution, as it will prevent requests from being streamlined in a performant manner.
This is a fairly gentle introduction to the topic if you’re already familiar with Python and have a familiarity with message broker systems.