On this page
queue
Prev: pyexpat | Up: stdlib_module | Next: quopri
Rel: os, io, time, argparse, getopt
Ref: docs.python.org — queue
Tags: #public #python3 #stdlib
Import
import queue
Overview
Thread-safe, synchronized FIFO (Queue), LIFO (LifoQueue), priority (PriorityQueue), and lightweight unbounded (SimpleQueue) queues for coordinating producer and consumer threads. Always prefer queue.Queue over sharing a plain list with a manual lock across threading workers.
Quick Start
import queue
q: queue.Queue[str] = queue.Queue(maxsize=2)
q.put("job-1")
q.put("job-2")
assert q.get() == "job-1"
q.task_done()
assert q.get() == "job-2"
q.task_done()
q.join()
Implementation Walkthrough
1. Producer / Consumer Worker Pattern with Sentinel Shutdown
Use a dedicated sentinel object (None or object()) and maxsize backpressure so producers cannot outrun consumers and exhaust memory:
import queue
import threading
STOP = object()
q: queue.Queue[object] = queue.Queue(maxsize=4)
results: list[int] = []
def worker() -> None:
while True:
item = q.get()
try:
if item is STOP:
return
assert isinstance(item, int)
results.append(item * 10)
finally:
q.task_done()
t = threading.Thread(target=worker)
t.start()
for n in (1, 2, 3):
q.put(n)
q.put(STOP)
q.join()
t.join()
assert results == [10, 20, 30]
Gotchas & Boundaries
- Do not use
queue.Queueinsideasynciocoroutines: Blocking.get()freezes the event loop; useasyncio.Queueinstead. - Never rely on
q.empty()orq.full()for control flow: Another thread can change the queue between the check and.get()/.put(); usetimeout=and catchqueue.Empty/queue.Full. - Always call
q.task_done()in afinally:block if any thread callsq.join(), orjoin()will block forever. - Python 3.13+ adds
q.shutdown()for cleanly signaling producers and consumers without manual sentinels.
Classes & Objects
- queue.Empty — Exception raised by Queue.get(block=0)/get_nowait(). (Methods:
.add_note(),.with_traceback()) - queue.Full — Exception raised by Queue.put(block=0)/put_nowait(). (Methods:
.add_note(),.with_traceback()) - queue.LifoQueue — Variant of Queue that retrieves most recently added entries first. (Methods:
.empty(),.full(),.get(),.get_nowait(),.join(),.put()) - queue.PriorityQueue — Variant of Queue that retrieves open entries in priority order (lowest first). (Methods:
.empty(),.full(),.get(),.get_nowait(),.join(),.put()) - queue.Queue — Create a queue object with a given maximum size. (Methods:
.empty(),.full(),.get(),.get_nowait(),.join(),.put()) - queue.ShutDown — Raised when put/get with shut-down queue. (Methods:
.add_note(),.with_traceback()) - queue.SimpleQueue — Simple, unbounded, reentrant FIFO queue. (Methods:
.empty(),.get(),.get_nowait(),.put(),.put_nowait(),.qsize())
Linked from
Related notes
- tuts/python3/stdlib_module/pyexpat Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/quopri Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/stdlib_module Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/argparse Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/getopt Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/io Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/os Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/queue/Empty Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/queue/Full Linked here; shared tags: #stdlib