Python › Concurrency (asyncio/threading/multiprocessing)

How would you implement a producer-consumer pattern combining a thread-safe queue.Queue with multiple worker threads?

Advanced

Answer

Use queue.Queue (thread-safe by design, unlike a plain list) — producer thread(s) call .put() to add work items, and multiple consumer/worker threads call .get() in a loop to pull and process items, with the queue's internal locking handling all synchronization automatically without you writing manual locks.
import threading, queue

q = queue.Queue()

def worker():
    while True:
        item = q.get()
        if item is None:
            break
        print(f"Processing {item}")
        q.task_done()

threads = [threading.Thread(target=worker) for _ in range(3)]
for t in threads: t.start()
for i in range(10): q.put(i)
for _ in threads: q.put(None)  # sentinel to stop each worker
for t in threads: t.join()
Real-world example Building a multi-threaded task processing system where several worker threads pull jobs from a shared, thread-safe queue.

Common follow-ups: How does queue.Queue's internal locking differ from what you'd need to write manually with threading.Lock alone?