Python

Producer / Consumer Queue (threading)

admin by @admin ADMIN
5d ago
May 31, 2026
Public
0 0 up · 0 down Sign in to vote
Classic producer / multi-consumer pattern using queue.Queue. Producers put work onto the queue; consumers pull and process; a sentinel value signals shutdown.
Python
Raw
import queue
import threading
import time

SENTINEL = object()
q: queue.Queue = queue.Queue(maxsize=100)

def producer(n: int):
    for i in range(n):
        q.put(f"job-{i}")
    q.put(SENTINEL)   # one sentinel per consumer

def consumer(name: str):
    while True:
        item = q.get()
        if item is SENTINEL:
            q.task_done()
            print(f"{name} done")
            return
        print(f"{name} -> {item}")
        time.sleep(0.05)
        q.task_done()

NUM_CONSUMERS = 3
threads = [threading.Thread(target=consumer, args=(f"c{i}",)) for i in range(NUM_CONSUMERS)]
for t in threads: t.start()

producer(20)
for _ in range(NUM_CONSUMERS - 1): q.put(SENTINEL)
for t in threads: t.join()
Tags

Save your own code snippets

Create a free account and build your private vault. Share publicly whenever you want.