Matt Howlett has an introduction to Apache Kafka, designed for Python developers:
In the call to the
produce
method, both thekey
andvalue
parameters 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 typeunicode
will 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 subclassProducer
and override theproduce
method 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
flush
method 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 theflush
method: if it is greater than zero, there are still produce commands that have yet to complete. Note that you should typically callflush
only 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.
Comments closed