On this page
queue.PriorityQueue
Prev: queue.LifoQueue | Up: queue | Next: queue.Queue
Rel: queue.Empty, queue.Full, queue.LifoQueue, queue.Queue
Ref: docs.python.org — queue.PriorityQueue
Tags: #public #python3 #stdlib #queue
Import
import queue
from queue import PriorityQueue
Signature & Overview
queue.PriorityQueue(maxsize=0)
Variant of Queue that retrieves open entries in priority order (lowest first).
Methods & Attributes
PriorityQueue.empty(self)— Return True if the queue is empty, False otherwise (not reliable!).PriorityQueue.full(self)— Return True if the queue is full, False otherwise (not reliable!).PriorityQueue.get(self, block=True, timeout=None)— Remove and return an item from the queue.PriorityQueue.get_nowait(self)— Remove and return an item from the queue without blocking.PriorityQueue.join(self)— Blocks until all items in the Queue have been gotten and processed.PriorityQueue.put(self, item, block=True, timeout=None)— Put an item into the queue.PriorityQueue.put_nowait(self, item)— Put an item into the queue without blocking.PriorityQueue.qsize(self)— Return the approximate size of the queue (not reliable!).PriorityQueue.shutdown(self, immediate=False)— Shut-down the queue, making queue gets and puts raise ShutDown.PriorityQueue.task_done(self)— Indicate that a formerly enqueued task is complete.
Usage
import queue
pq: queue.PriorityQueue[tuple[int, str]] = queue.PriorityQueue()
pq.put((2, "medium"))
pq.put((1, "urgent"))
assert pq.get() == (1, "urgent")
Gotchas
- Lowest value pops first (min-heap): If priorities can tie and payloads aren't comparable (like
dicts), store(priority, seq_num, payload)so ties break onseq_numinstead of raisingTypeError.
REPL Inspect
from queue import PriorityQueue
help(PriorityQueue)
Linked from
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/Queue Linked here; links to this page; shared tags: #queue, #stdlib
- tuts/python3/stdlib_module/queue/ShutDown 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