Python queue - thread-safe FIFO, LIFO and priority queues with task_done and join
Answer: Use queue.Queue to pass work between threads: producers call put(), consumers call get() then task_done(), and join() blocks until every item is processed. The queue module implements multi-producer, multi-consumer queues with all the required locking semantics.
Example
import queue
import threading
q = queue.Queue(maxsize=100)
def worker():
while True:
item = q.get()
print(f"working on {item}")
q.task_done()
threading.Thread(target=worker, daemon=True).start()
for item in range(30):
q.put(item)
q.join() # blocks until all items are processed
print("all work completed")
pq = queue.PriorityQueue()
pq.put((2, "low"))
pq.put((1, "high"))
pq.get() # (1, 'high')
try:
q.get_nowait()
except queue.Empty:
pass
Queue types
| Class | Order | |---|---| | Queue(maxsize=0) | FIFO: the first tasks added are the first retrieved | | LifoQueue(maxsize=0) | LIFO: the most recently added entry first, like a stack | | PriorityQueue(maxsize=0) | lowest valued entry first, kept sorted with heapq | | SimpleQueue() | unbounded FIFO without task tracking |
maxsize caps the number of items; put() blocks once it is reached. If maxsize is less than or equal to zero, the queue size is infinite.
Rules
- For priority queues, use
(priority_number, data)tuples. If data is not comparable, wrap it in a dataclass withorder=Trueandfield(compare=False). get_nowait()on an empty queue raisesqueue.Empty;put_nowait()on a full queue raisesqueue.Full.qsize()is approximate:qsize() > 0doesn't guarantee that a subsequentget()will not block.- Each
get()should be followed bytask_done();join()unblocks when the count of unfinished tasks drops to zero. shutdown()(3.13+) stops the queue from growing; laterput()calls raiseShutDown.- The queues are not designed to handle reentrancy within a thread.
See also
Claims
- id: C1 claim: The queue module implements multi-producer, multi-consumer queues. source: S1 evidence: "The queue module implements multi-producer, multi-consumer queues."
- id: C2 claim: qsize is approximate. source: S1 evidence: "Return the approximate size of the queue."
- id: C3 claim: A maxsize of zero or less means an infinite queue. source: S2 evidence: "If maxsize is <= 0, the queue size is infinite."
Sources
- [S1] queue - A synchronized queue class. Python docs. https://docs.python.org/3/library/queue.html . License: PSF-2.0. Accessed 2026-11-18.
- [S2] Lib/queue.py. python/cpython (GitHub). https://github.com/python/cpython/blob/main/Lib/queue.py . License: PSF-2.0. Accessed 2026-11-18.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Python docs, queue - A synchronized queue class; python/cpython (GitHub), Lib/queue.py. PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation. Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Python docs, queue - A synchronized queue class; python/cpython (GitHub), Lib/queue.py. PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation.
Cite this
iinventedlight. 2026. "Python queue - thread-safe FIFO, LIFO and priority queues with task_done and join." iinventedlight Library, iil:note:jsx18b1k, version of 2026-11-18. https://iinventedlight.com/library/notes/python-queue/. License: CC-BY-4.0.
@misc{iil_note_jsx18b1k,
author = {{iinventedlight}},
title = {{Python queue - thread-safe FIFO, LIFO and priority queues with task\_done and join}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-18},
url = {https://iinventedlight.com/library/notes/python-queue/},
note = {Permanent ID iil:note:jsx18b1k. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
Extra files beside this note
These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.
- python-queue.table.json (table; CC-BY-4.0)
- python-queue.schema.json (schema; CC-BY-4.0)
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/python-queue.md.