Sub topic 2
Producers and consumers
This chapter assumes you can read basic Python, understand HTTP request/response, and know that threads run concurrently. It covers the two roles at the ends of every messaging system and the shape of the code on each side.
The two roles
A producer is a program that creates a message and hands it off. A consumer is a program that receives a message and does the work it describes. The producer does not call the consumer. It puts the message somewhere both can reach, then moves on.
This differs from a synchronous call, where the caller blocks until the callee answers. A producer only needs to know that its message was accepted. It does not wait for the work to finish.
A minimal example
A queue inside one process shows the shape without a broker. The producer puts jobs on the queue. The consumer, running in a thread, takes them off one at a time.
import queue
import threading
q = queue.Queue()
def consumer():
while True:
item = q.get()
if item is None:
q.task_done()
break
print("processing", item)
q.task_done()
t = threading.Thread(target=consumer)
t.start()
for i in range(3):
q.put(f"job-{i}")
q.put(None)
q.join()
t.join()The main thread is the producer. Each q.put returns immediately, even while the consumer is busy. The consumer loops on q.get, which blocks until a message is available. The value None is a sentinel, a message that means "stop". task_done tells the queue that one item is finished, and q.join() waits until every item is accounted for.
What each side is responsible for
| Concern | Producer | Consumer |
|---|---|---|
| Main action | Creates and sends messages | Receives and processes messages |
| Waits for | Acceptance by the queue | A message to arrive |
| Knows about the other side | Nothing, only the queue | Nothing, only the queue |
| Typical failure | Cannot reach the queue | Crashes while processing |
Neither side knows the other's address, language, or speed. That is the decoupling. You can deploy, restart, or replace one side without touching the other, as long as the message format stays compatible.
Common errors
Most early bugs come from treating the queue like a function call.
- Expecting a result. A producer that needs an answer must send a second message back, or use a synchronous call instead.
- Slow consumers. If producers add messages faster than the consumer removes them, the queue grows. Later chapters cover prefetch limits and monitoring for this.
- No stop signal. A consumer that loops forever needs a way to exit cleanly, like the
Nonesentinel above. - Treating "sent" as "done". A successful
putmeans the message is queued. It does not mean the work happened.
Summary
A producer sends and continues. A consumer receives and works. The queue between them absorbs differences in timing. Everything else in messaging, including reliability and scaling, builds on this split.
Exercise
In the example above, the producer calls q.put three times and the consumer takes a long time on each job. Does the producer's loop slow down? What happens to the jobs that are not yet processed?
Show answer
The loop does not slow down, because q.put returns immediately and does not wait for the consumer. The unprocessed jobs wait in the queue and are handled one at a time in the order they were added.