The queue module provides thread-safe queues for passing work and data between threads. Producers call put(), consumers call get(), and the queue handles all locking internally, so you do not need your own Lock or Condition. It offers FIFO (Queue), LIFO (LifoQueue), priority (PriorityQueue) and lightweight (SimpleQueue) variants.
Use it for the producer-consumer pattern: one or more threads create tasks, a pool of worker threads processes them, and the queue decouples the two sides and can limit how much work is waiting.
Queue, LifoQueue and PriorityQueueQueuequeue, multiprocessing and asyncio| Class | Order of retrieval | Notes |
|---|---|---|
queue.Queue | First in, first out | The default choice; supports maxsize, task_done() and join() |
queue.LifoQueue | Last in, first out | Behaves like a thread-safe stack |
queue.PriorityQueue | Lowest value first | Items must be comparable, usually (priority, item) tuples |
queue.SimpleQueue | First in, first out | Unbounded, no task_done(), safe to use from signal handlers and __del__ |
All of them live in the standard library, and all except SimpleQueue accept a maxsize argument. A maxsize of 0 or less means unbounded.
Queue, LifoQueue and PriorityQueueFor PriorityQueue, a lower number means higher priority. If two items share a priority and the second elements cannot be compared (for example two dictionaries), Python raises TypeError. Add a unique counter as a tiebreaker: (priority, next(counter), item).
QueueA common mistake is to loop with while not q.empty(). Between the check and the get(), another thread may take the last item, and the first thread then blocks forever. The reliable pattern is to block on get() and tell workers to stop with a sentinel value.
How it works:
tasks.get() until work arrives.task_done() tells the queue that the item fetched by get() is fully processed. It runs in a finally block so a failing task cannot stall join().tasks.join() blocks until the number of task_done() calls equals the number of items put.In Python 3.13 and later you can call queue.shutdown() instead of sending sentinels. After shutdown, put() raises queue.ShutDown, and get() raises it once the queue is empty, which workers can catch to exit.
put() and get() block by default. You can change that with arguments.
queue.Full and queue.Empty are the exceptions to catch. Methods such as qsize(), empty() and full() are only approximate in a multi-threaded program, because the state can change right after the call. Use them for monitoring, not for control flow.
queue, multiprocessing and asyncio| Situation | Use |
|---|---|
| Threads in one process | queue.Queue |
| Separate processes | multiprocessing.Queue or multiprocessing.Manager().Queue() |
| Coroutines on an event loop | asyncio.Queue, which is not thread-safe |
| Simple "run these functions in a pool" | concurrent.futures.ThreadPoolExecutor, which uses a queue internally |
Do not use queue.Queue inside coroutines, because a blocking get() freezes the whole event loop. Do not use collections.deque as a replacement when you need blocking and join(); deque appends and pops are thread-safe, but it has no blocking waits.
queue.Queue thread-safe?Yes. All its methods acquire internal locks, so many threads can put and get at the same time without corrupting the queue.
queue.Queue between processes?No, it only works within one process. Use multiprocessing.Queue to share data between processes.
Put one sentinel value per worker, then call join(). On Python 3.13 and later, call queue.shutdown(). Marking threads as daemon threads is a last resort, because they can be killed in the middle of a task.
task_done() do?It decrements the count of unfinished tasks. When the count reaches zero, any thread blocked in join() is released. Call it exactly once for every item you take with get().
The queue module gives you thread-safe FIFO, LIFO, priority and simple queues. Use put() and get() with a bounded maxsize for back-pressure, task_done() and join() to wait for completion, and sentinels (or shutdown() on Python 3.13+) to stop workers. Avoid empty() checks for control flow, and choose asyncio.Queue or multiprocessing.Queue outside plain threading.