Courses/Concurrency & Parallelism

Concurrency & Parallelism

Producer / Consumer with Queue

Thread-safe coordination with queue.Queue.

queue.Queue is the canonical way to hand work between threads safely.

import queue, threading

q = queue.Queue()

def consumer():
    while True:
        item = q.get()
        if item is None: break
        print("got", item)
        q.task_done()
main.py
Output
Press Run to execute.
Expected output
a
b
c

Sign in to track your progress across lessons.