On this page
queue.Queue
Prev: queue.PriorityQueue | Up: queue | Next: queue.ShutDown
Rel: queue.Empty, queue.Full, queue.LifoQueue, queue.PriorityQueue
Ref: docs.python.org — queue.Queue
Tags: #public #python3 #stdlib #queue
Import
import queue
from queue import Queue
Signature & Overview
queue.Queue(maxsize=0)
Create a queue object with a given maximum size.
Methods & Attributes
Queue.empty(self)— Return True if the queue is empty, False otherwise (not reliable!).Queue.full(self)— Return True if the queue is full, False otherwise (not reliable!).Queue.get(self, block=True, timeout=None)— Remove and return an item from the queue.Queue.get_nowait(self)— Remove and return an item from the queue without blocking.Queue.join(self)— Blocks until all items in the Queue have been gotten and processed.Queue.put(self, item, block=True, timeout=None)— Put an item into the queue.Queue.put_nowait(self, item)— Put an item into the queue without blocking.Queue.qsize(self)— Return the approximate size of the queue (not reliable!).Queue.shutdown(self, immediate=False)— Shut-down the queue, making queue gets and puts raise ShutDown.Queue.task_done(self)— Indicate that a formerly enqueued task is complete.
Usage
import queue
q: queue.Queue[str] = queue.Queue(maxsize=10)
q.put("task-1")
item = q.get(timeout=1)
q.task_done()
q.join()
assert item == "task-1"
Gotchas
- For threads, NOT processes or
asyncio: Usemultiprocessing.Queueacross processes andasyncio.Queueinside coroutines. - Avoid
q.empty()/q.full()for flow control: They are racy in multi-threaded code—useget(timeout=...)/put(timeout=...)and catchEmpty/Full.
REPL Inspect
from queue import Queue
help(Queue)
Linked from
- tuts/python3/stdlib_module/queue
- tuts/python3/stdlib_module/queue/Empty
- tuts/python3/stdlib_module/queue/Full
- tuts/python3/stdlib_module/queue/LifoQueue
- tuts/python3/stdlib_module/queue/PriorityQueue
- tuts/python3/stdlib_module/queue/Queue
- tuts/python3/stdlib_module/queue/ShutDown
- tuts/python3/stdlib_module/queue/SimpleQueue
Related notes
- tuts/python3/stdlib_module/queue/Empty Linked here; links to this page; shared tags: #queue, #stdlib
- tuts/python3/stdlib_module/queue/Full Linked here; links to this page; shared tags: #queue, #stdlib
- tuts/python3/stdlib_module/queue/LifoQueue Linked here; links to this page; shared tags: #queue, #stdlib
- tuts/python3/stdlib_module/queue/PriorityQueue Linked here; links to this page; shared tags: #queue, #stdlib
- tuts/python3/stdlib_module/queue/ShutDown Linked here; links to this page; shared tags: #queue, #stdlib
- tuts/python3/stdlib_module/queue/SimpleQueue links to this page; shared tags: #queue, #stdlib
- tuts/python3/stdlib_module/queue links to this page; shared tags: #stdlib
- tuts/c/stdlib shared tags: #stdlib
- tuts/c/stdlib/assert shared tags: #stdlib
- tuts/c/stdlib/assert/assert shared tags: #stdlib