DeAI Research
Source · pydoc

queue — A synchronized queue class — Python 3.13.18 documentation

2001

cid 37a8628354c0d21a338f4bf5976056fd0044e69b0581c2604d55d715471e8bd8 · original · open access · via pydoc

Title: queue — A synchronized queue class — Python 3.13.18 documentation
Authors: 
Venue: pydoc
Year: 2001
Canonical: pydoc:queue@3.13
URL: https://docs.python.org/3.13/library/queue.html
Collector: pydoc
OA: true

ABSTRACT
queue — A synchronized queue class — Python 3.13.18 documentation Navigation index modules | next | previous | Python » 3.13.18 Documentation » The Python standard library » Concurrent Execution » queue — A synchronized queue class | Theme Auto Light Dark | queue — A synchronized queue class¶ Source code: Lib/queue.py The queue module implements multi-producer, multi-consumer queues. It is especially useful in threaded programming when information must be exchanged safely between multiple threads. The Queue class in this module implements all the required locking semantics. The module implements three types of queue, which differ only in the order in which the entries are retrieved. In a FIFO queue, the first tasks added are the first retrieved. In a LIFO queue, the most recently added entry is the first retrieved (operating like a stack). With a priority queue, the entries are kept sorted (using the heapq module) and the lowest valued entry is retrieved first. Internally, those three types of queues use locks to temporarily block competing threads; however, they are not designed to handle reentrancy within a thread. In addition, the module implements a “simple” FIFO queue type, SimpleQueue, whose specific implementation provides additional guarantees in exchange for the smaller functionality. The queue module defines the following classes and exceptions: class queue.Queue(maxsize=0)¶ Constructor for a FIFO queue. maxsize is an integer that sets the upperbound limit on the number of items that can be placed in the queue. Insertion will block once this size has been reached, until queue items are consumed. If maxsize is less than or equal to zero, the queue size is infinite. class queue.LifoQueue(maxsize=0)¶ Constructor for a LIFO queue. maxsize is an integer that sets the upperbound limit on the number of items that can be placed in the queue. Insertion will block once this size has been reached, until queue items are consumed. If maxsize is less than or equal to zero, the queue size is infinite. class queue.PriorityQueue(maxsize=0)¶ Constructor for a priority queue. maxsize is an integer that sets the upperbound limit on the number of items that can be placed in the queue. Insertion will block once this size has been reached, until queue items are consumed. If maxsize is less than or equal to zero, the queue size is infinite. The lowest valued entries are retrieved first (the lowest valued entry is the one that would be returned by min(entries)). A typical pattern for entries is a tuple in the form: (priority_number, data). If the data elements are not comparable, the data can be wrapped in a class that ignores the data item and only compares the priority number: from dataclasses import dataclass, field from typing import Any @dataclass(order=True) class PrioritizedItem: priority: int item: Any=field(compare=False) class queue.SimpleQueue¶ Constructor for an unbounded FIFO queue. Simple queues lack advanced functionality such as task tracking. Simple queues are generic over the type of their items. Added in version 3.7. exception queue.Empty¶ Exception raised when non-blocking get() (or get_nowait()) is called on a Queue object which is empty. exception queue.Full¶ Exception raised when non-blocking put() (or put_nowait()) is called on a Queue object which is full. exception queue.ShutDown¶ Exception raised when put() or get() is called on a Queue object which has been shut down. Added in version 3.13. Queue Objects¶ Queue objects (Queue, LifoQueue, or PriorityQueue) provide the public methods described below. Queue.qsize()¶ Return the approximate size of the queue. Note, qsize() 0 doesn’t guarantee that a subsequent get() will not block, nor will qsize() 0 doesn’t guarantee that a subsequent get() will not block. SimpleQueue.empty()¶ Return True if the queue is empty, False otherwise. If empty() returns False it doesn’t guarantee that a subsequent call to get() will not block. SimpleQueue.put(item, block=True, timeout=None)¶ Put item into the queue. The method never blocks and always succeeds (except for potential low-level errors such as failure to allocate memory). The optional args block and timeout are ignored and only provided for compatibility with Queue.put(). CPython implementation detail: This method has a C implementation which is reentrant. That is, a put() or get() call can be interrupted by another put() call in the same thread without deadlocking or corrupting internal state inside the queue. This makes it appropriate for use in destructors such as __del__ methods or weakref callbacks. SimpleQueue.put_nowait(item)¶ Equivalent to put(item, block=False), provided for compatibility with Queue.put_nowait(). SimpleQueue.get(block=True, timeout=None)¶ Remove and return an item from the queue. If optional args block is true and timeout is None (the default), block if necessary until an item is available. If timeout is a positive number, it blocks at most timeout seconds and raises the Empty exception if no item was available within that time. Otherwise (block is false), return an item if one is immediately available, else raise the Empty exception (timeout is ignored in that case). SimpleQueue.get_nowait()¶ Equivalent to get(False). See also Class multiprocessing.QueueA queue class for use in a multi-processing (rather than multi-threading) context. collections.deque is an alternative implementation of unbounded queues with fast atomic append() and popleft() operations that do not require locking and also support indexing. Table of Contents queue — A synchronized queue class Queue Objects Waiting for task completion Terminating queues SimpleQueue Objects Previous topic sched — Event scheduler Next topic contextvars — Context Variables This page Report a bug Show source « Navigation index modules | next | previous | Python » 3.13.18 Documentation » The Python standard library » Concurrent Execution » queue — A synchronized queue class | Theme Auto Light Dark | © Copyright 2001-2026, Python Software Foundation. This page is licensed under the Python Software Foundation License Version 2. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License. See History and License for more information. The Python Software Foundation is a non-profit corporation. Please donate. Last updated on Oct 26, 2026 (13:10 UTC). Found a bug? Created using Sphinx 8.2.3.