Bird
Raised Fist0

What will happen when the following Python code using confluent-kafka producer is executed?

medium📝 Predict Output Q4 of Q15
Kafka - with Java/Python
What will happen when the following Python code using confluent-kafka producer is executed?
from confluent_kafka import Producer

producer = Producer({'bootstrap.servers': 'localhost:9092'})
producer.produce('my_topic', value='hello')
producer.poll(0)
producer.flush()
AThe message is queued but not sent because <code>poll()</code> is called with 0 timeout.
BThe message 'hello' is sent to 'my_topic' and delivery is confirmed before program exits.
CAn error occurs because <code>flush()</code> must be called before <code>produce()</code>.
DThe producer raises an exception due to missing key parameter in <code>produce()</code>.
Step-by-Step Solution
Solution:
  1. Step 1: Create Producer instance

    The producer is configured with the Kafka broker address.
  2. Step 2: Produce message

    The produce() method queues the message to be sent asynchronously.
  3. Step 3: Call poll(0)

    This serves delivery callbacks without blocking; 0 means non-blocking call.
  4. Step 4: Call flush()

    This blocks until all messages are delivered or timeout occurs.
  5. Final Answer:

    The message 'hello' is sent and delivery is confirmed before exit. -> Option B
  6. Quick Check:

    Calling flush() ensures delivery [OK]
Quick Trick: Always call flush() to ensure message delivery [OK]
Common Mistakes:
MISTAKES
  • Assuming poll(0) sends messages immediately
  • Not calling flush() before program exit
  • Expecting produce() to block until delivery

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kafka Quizzes