← All notes

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

See also

Claims

Sources

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

Permanent ID: iil:note:jsx18b1k

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.},
}

Markdown

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.

All artifact files (JSON index)

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.