0
0
Kafkadevops~10 mins

Python producer (confluent-kafka) - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Python producer (confluent-kafka)
Create Producer Config
Initialize Producer
Prepare Message
Call produce()
Message Queued
Call flush() to send
Message Sent to Kafka
Exit
This flow shows how a Python Kafka producer is configured, sends a message, and ensures delivery.
Execution Sample
Kafka
from confluent_kafka import Producer

p = Producer({'bootstrap.servers': 'localhost:9092'})
p.produce('my_topic', key='key1', value='hello')
p.flush()
This code creates a Kafka producer, sends one message with key and value, then waits for delivery.
Process Table
StepActionInternal StateResult/Output
1Create Producer with configProducer object created, connected to localhost:9092Ready to send messages
2Call produce() with topic='my_topic', key='key1', value='hello'Message queued in producer bufferNo immediate output
3Call flush()Producer sends all queued messages to Kafka brokerMessage delivered to 'my_topic'
4ExitNo pending messagesProgram ends
💡 All messages flushed and sent, producer cleanly closed
Status Tracker
VariableStartAfter produce()After flush()Final
pNoneProducer object with 1 message queuedProducer object with empty queueProducer object closed
message_queueEmpty1 message queuedEmptyEmpty
Key Moments - 3 Insights
Why do we need to call flush() after produce()?
produce() only queues the message; flush() actually sends all queued messages to Kafka. See execution_table step 3.
What happens if we call produce() without flush()?
Messages may stay in the buffer and not be sent, so flush() ensures delivery before program ends (execution_table step 4).
Why do we pass 'bootstrap.servers' in the config?
It tells the producer where the Kafka broker is, so it can connect and send messages (execution_table step 1).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the state of the producer after calling produce()?
AProducer closed
BMessage sent to Kafka broker
CMessage queued in producer buffer
DNo messages queued
💡 Hint
Check execution_table row 2 under 'Internal State'
At which step does the message actually get sent to Kafka?
AStep 3
BStep 1
CStep 2
DStep 4
💡 Hint
Look at execution_table row 3 'Result/Output'
If flush() is not called, what is the likely outcome?
AMessage is sent immediately
BMessage stays queued and may not be sent
CProducer throws an error
DMessage is lost before queuing
💡 Hint
Refer to key_moments about flush() importance and execution_table step 4
Concept Snapshot
Python Kafka Producer (confluent-kafka):
- Create Producer with config {'bootstrap.servers': 'host:port'}
- Use produce(topic, key, value) to queue messages
- Call flush() to send all queued messages
- flush() ensures delivery before program ends
- Without flush(), messages may not be sent
Full Transcript
This visual trace shows how to use the Python confluent-kafka Producer. First, you create a Producer object with the Kafka broker address. Then, you call produce() to queue a message with a topic, key, and value. The message is not sent immediately but stored in a buffer. To actually send the message to Kafka, you call flush(), which sends all queued messages and waits for confirmation. Finally, the program ends after all messages are delivered. Remember, without flush(), messages may remain unsent. This step-by-step helps beginners see how the producer works internally and why flush() is important.