---
source: https://iinventedlight.com/library/notes/python-queue.md
title: "Python queue - thread-safe FIFO, LIFO and priority queues with task_done and join"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "PSF-2.0 (Python docs); PSF-2.0 (python/cpython (GitHub)); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "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."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-18
status: published
approved_by: "Cole Withington, 2026-11-20 13:14 PT: \"Publish the full batch and rewrite the 6 live notes\""
checked_on: 2026-11-18
as_of: 2026-11-18
confidence: medium
untested: "Not reproduced (2026-11-18). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-python: queue"
tags: [python, queue, threading, concurrency, producer-consumer, stdlib]
client: Librarian
sources:
  - id: S1
    title: "queue - A synchronized queue class"
    publisher: Python docs
    url: https://docs.python.org/3/library/queue.html
    accessed: 2026-11-18
    license: "PSF-2.0"
  - id: S2
    title: "Lib/queue.py"
    publisher: python/cpython (GitHub)
    url: https://github.com/python/cpython/blob/main/Lib/queue.py
    accessed: 2026-11-18
    license: "PSF-2.0"
---
# 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

```python
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 with `order=True` and `field(compare=False)`.
- `get_nowait()` on an empty queue raises `queue.Empty`; `put_nowait()` on a full queue raises `queue.Full`.
- `qsize()` is approximate: `qsize() > 0` doesn't guarantee that a subsequent `get()` will not block.
- Each `get()` should be followed by `task_done()`; `join()` unblocks when the count of unfinished tasks drops to zero.
- `shutdown()` (3.13+) stops the queue from growing; later `put()` calls raise `ShutDown`.
- The queues are not designed to handle reentrancy within a thread.

## See also

- [python-asyncio-basics](https://iinventedlight.com/library/notes/python-asyncio-basics/)

## 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.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
