summaryrefslogtreecommitdiff
path: root/devdocs/python~3.12/library%2Fqueue.html
diff options
context:
space:
mode:
Diffstat (limited to 'devdocs/python~3.12/library%2Fqueue.html')
-rw-r--r--devdocs/python~3.12/library%2Fqueue.html102
1 files changed, 102 insertions, 0 deletions
diff --git a/devdocs/python~3.12/library%2Fqueue.html b/devdocs/python~3.12/library%2Fqueue.html
new file mode 100644
index 00000000..8b6bff15
--- /dev/null
+++ b/devdocs/python~3.12/library%2Fqueue.html
@@ -0,0 +1,102 @@
+ <span id="queue-a-synchronized-queue-class"></span><h1>queue — A synchronized queue class</h1> <p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Lib/queue.py">Lib/queue.py</a></p> <p>The <a class="reference internal" href="#module-queue" title="queue: A synchronized queue class."><code>queue</code></a> module implements multi-producer, multi-consumer queues. It is especially useful in threaded programming when information must be exchanged safely between multiple threads. The <a class="reference internal" href="#queue.Queue" title="queue.Queue"><code>Queue</code></a> class in this module implements all the required locking semantics.</p> <p>The module implements three types of queue, which differ only in the order in which the entries are retrieved. In a <abbr title="first-in, first-out">FIFO</abbr> queue, the first tasks added are the first retrieved. In a <abbr title="last-in, first-out">LIFO</abbr> 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 <a class="reference internal" href="heapq#module-heapq" title="heapq: Heap queue algorithm (a.k.a. priority queue)."><code>heapq</code></a> module) and the lowest valued entry is retrieved first.</p> <p>Internally, those three types of queues use locks to temporarily block competing threads; however, they are not designed to handle reentrancy within a thread.</p> <p>In addition, the module implements a “simple” <abbr title="first-in, first-out">FIFO</abbr> queue type, <a class="reference internal" href="#queue.SimpleQueue" title="queue.SimpleQueue"><code>SimpleQueue</code></a>, whose specific implementation provides additional guarantees in exchange for the smaller functionality.</p> <p>The <a class="reference internal" href="#module-queue" title="queue: A synchronized queue class."><code>queue</code></a> module defines the following classes and exceptions:</p> <dl class="py class"> <dt class="sig sig-object py" id="queue.Queue">
+<code>class queue.Queue(maxsize=0)</code> </dt> <dd>
+<p>Constructor for a <abbr title="first-in, first-out">FIFO</abbr> queue. <em>maxsize</em> 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 <em>maxsize</em> is less than or equal to zero, the queue size is infinite.</p> </dd>
+</dl> <dl class="py class"> <dt class="sig sig-object py" id="queue.LifoQueue">
+<code>class queue.LifoQueue(maxsize=0)</code> </dt> <dd>
+<p>Constructor for a <abbr title="last-in, first-out">LIFO</abbr> queue. <em>maxsize</em> 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 <em>maxsize</em> is less than or equal to zero, the queue size is infinite.</p> </dd>
+</dl> <dl class="py class"> <dt class="sig sig-object py" id="queue.PriorityQueue">
+<code>class queue.PriorityQueue(maxsize=0)</code> </dt> <dd>
+<p>Constructor for a priority queue. <em>maxsize</em> 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 <em>maxsize</em> is less than or equal to zero, the queue size is infinite.</p> <p>The lowest valued entries are retrieved first (the lowest valued entry is the one that would be returned by <code>min(entries)</code>). A typical pattern for entries is a tuple in the form: <code>(priority_number, data)</code>.</p> <p>If the <em>data</em> elements are not comparable, the data can be wrapped in a class that ignores the data item and only compares the priority number:</p> <pre data-language="python">from dataclasses import dataclass, field
+from typing import Any
+
+@dataclass(order=True)
+class PrioritizedItem:
+ priority: int
+ item: Any=field(compare=False)
+</pre> </dd>
+</dl> <dl class="py class"> <dt class="sig sig-object py" id="queue.SimpleQueue">
+<code>class queue.SimpleQueue</code> </dt> <dd>
+<p>Constructor for an unbounded <abbr title="first-in, first-out">FIFO</abbr> queue. Simple queues lack advanced functionality such as task tracking.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.7.</span></p> </div> </dd>
+</dl> <dl class="py exception"> <dt class="sig sig-object py" id="queue.Empty">
+<code>exception queue.Empty</code> </dt> <dd>
+<p>Exception raised when non-blocking <a class="reference internal" href="#queue.Queue.get" title="queue.Queue.get"><code>get()</code></a> (or <a class="reference internal" href="#queue.Queue.get_nowait" title="queue.Queue.get_nowait"><code>get_nowait()</code></a>) is called on a <a class="reference internal" href="#queue.Queue" title="queue.Queue"><code>Queue</code></a> object which is empty.</p> </dd>
+</dl> <dl class="py exception"> <dt class="sig sig-object py" id="queue.Full">
+<code>exception queue.Full</code> </dt> <dd>
+<p>Exception raised when non-blocking <a class="reference internal" href="#queue.Queue.put" title="queue.Queue.put"><code>put()</code></a> (or <a class="reference internal" href="#queue.Queue.put_nowait" title="queue.Queue.put_nowait"><code>put_nowait()</code></a>) is called on a <a class="reference internal" href="#queue.Queue" title="queue.Queue"><code>Queue</code></a> object which is full.</p> </dd>
+</dl> <section id="queue-objects"> <span id="queueobjects"></span><h2>Queue Objects</h2> <p>Queue objects (<a class="reference internal" href="#queue.Queue" title="queue.Queue"><code>Queue</code></a>, <a class="reference internal" href="#queue.LifoQueue" title="queue.LifoQueue"><code>LifoQueue</code></a>, or <a class="reference internal" href="#queue.PriorityQueue" title="queue.PriorityQueue"><code>PriorityQueue</code></a>) provide the public methods described below.</p> <dl class="py method"> <dt class="sig sig-object py" id="queue.Queue.qsize">
+<code>Queue.qsize()</code> </dt> <dd>
+<p>Return the approximate size of the queue. Note, qsize() &gt; 0 doesn’t guarantee that a subsequent get() will not block, nor will qsize() &lt; maxsize guarantee that put() will not block.</p> </dd>
+</dl> <dl class="py method"> <dt class="sig sig-object py" id="queue.Queue.empty">
+<code>Queue.empty()</code> </dt> <dd>
+<p>Return <code>True</code> if the queue is empty, <code>False</code> otherwise. If empty() returns <code>True</code> it doesn’t guarantee that a subsequent call to put() will not block. Similarly, if empty() returns <code>False</code> it doesn’t guarantee that a subsequent call to get() will not block.</p> </dd>
+</dl> <dl class="py method"> <dt class="sig sig-object py" id="queue.Queue.full">
+<code>Queue.full()</code> </dt> <dd>
+<p>Return <code>True</code> if the queue is full, <code>False</code> otherwise. If full() returns <code>True</code> it doesn’t guarantee that a subsequent call to get() will not block. Similarly, if full() returns <code>False</code> it doesn’t guarantee that a subsequent call to put() will not block.</p> </dd>
+</dl> <dl class="py method"> <dt class="sig sig-object py" id="queue.Queue.put">
+<code>Queue.put(item, block=True, timeout=None)</code> </dt> <dd>
+<p>Put <em>item</em> into the queue. If optional args <em>block</em> is true and <em>timeout</em> is <code>None</code> (the default), block if necessary until a free slot is available. If <em>timeout</em> is a positive number, it blocks at most <em>timeout</em> seconds and raises the <a class="reference internal" href="#queue.Full" title="queue.Full"><code>Full</code></a> exception if no free slot was available within that time. Otherwise (<em>block</em> is false), put an item on the queue if a free slot is immediately available, else raise the <a class="reference internal" href="#queue.Full" title="queue.Full"><code>Full</code></a> exception (<em>timeout</em> is ignored in that case).</p> </dd>
+</dl> <dl class="py method"> <dt class="sig sig-object py" id="queue.Queue.put_nowait">
+<code>Queue.put_nowait(item)</code> </dt> <dd>
+<p>Equivalent to <code>put(item, block=False)</code>.</p> </dd>
+</dl> <dl class="py method"> <dt class="sig sig-object py" id="queue.Queue.get">
+<code>Queue.get(block=True, timeout=None)</code> </dt> <dd>
+<p>Remove and return an item from the queue. If optional args <em>block</em> is true and <em>timeout</em> is <code>None</code> (the default), block if necessary until an item is available. If <em>timeout</em> is a positive number, it blocks at most <em>timeout</em> seconds and raises the <a class="reference internal" href="#queue.Empty" title="queue.Empty"><code>Empty</code></a> exception if no item was available within that time. Otherwise (<em>block</em> is false), return an item if one is immediately available, else raise the <a class="reference internal" href="#queue.Empty" title="queue.Empty"><code>Empty</code></a> exception (<em>timeout</em> is ignored in that case).</p> <p>Prior to 3.0 on POSIX systems, and for all versions on Windows, if <em>block</em> is true and <em>timeout</em> is <code>None</code>, this operation goes into an uninterruptible wait on an underlying lock. This means that no exceptions can occur, and in particular a SIGINT will not trigger a <a class="reference internal" href="exceptions#KeyboardInterrupt" title="KeyboardInterrupt"><code>KeyboardInterrupt</code></a>.</p> </dd>
+</dl> <dl class="py method"> <dt class="sig sig-object py" id="queue.Queue.get_nowait">
+<code>Queue.get_nowait()</code> </dt> <dd>
+<p>Equivalent to <code>get(False)</code>.</p> </dd>
+</dl> <p>Two methods are offered to support tracking whether enqueued tasks have been fully processed by daemon consumer threads.</p> <dl class="py method"> <dt class="sig sig-object py" id="queue.Queue.task_done">
+<code>Queue.task_done()</code> </dt> <dd>
+<p>Indicate that a formerly enqueued task is complete. Used by queue consumer threads. For each <a class="reference internal" href="#queue.Queue.get" title="queue.Queue.get"><code>get()</code></a> used to fetch a task, a subsequent call to <a class="reference internal" href="#queue.Queue.task_done" title="queue.Queue.task_done"><code>task_done()</code></a> tells the queue that the processing on the task is complete.</p> <p>If a <a class="reference internal" href="#queue.Queue.join" title="queue.Queue.join"><code>join()</code></a> is currently blocking, it will resume when all items have been processed (meaning that a <a class="reference internal" href="#queue.Queue.task_done" title="queue.Queue.task_done"><code>task_done()</code></a> call was received for every item that had been <a class="reference internal" href="#queue.Queue.put" title="queue.Queue.put"><code>put()</code></a> into the queue).</p> <p>Raises a <a class="reference internal" href="exceptions#ValueError" title="ValueError"><code>ValueError</code></a> if called more times than there were items placed in the queue.</p> </dd>
+</dl> <dl class="py method"> <dt class="sig sig-object py" id="queue.Queue.join">
+<code>Queue.join()</code> </dt> <dd>
+<p>Blocks until all items in the queue have been gotten and processed.</p> <p>The count of unfinished tasks goes up whenever an item is added to the queue. The count goes down whenever a consumer thread calls <a class="reference internal" href="#queue.Queue.task_done" title="queue.Queue.task_done"><code>task_done()</code></a> to indicate that the item was retrieved and all work on it is complete. When the count of unfinished tasks drops to zero, <a class="reference internal" href="#queue.Queue.join" title="queue.Queue.join"><code>join()</code></a> unblocks.</p> </dd>
+</dl> <p>Example of how to wait for enqueued tasks to be completed:</p> <pre data-language="python">import threading
+import queue
+
+q = queue.Queue()
+
+def worker():
+ while True:
+ item = q.get()
+ print(f'Working on {item}')
+ print(f'Finished {item}')
+ q.task_done()
+
+# Turn-on the worker thread.
+threading.Thread(target=worker, daemon=True).start()
+
+# Send thirty task requests to the worker.
+for item in range(30):
+ q.put(item)
+
+# Block until all tasks are done.
+q.join()
+print('All work completed')
+</pre> </section> <section id="simplequeue-objects"> <h2>SimpleQueue Objects</h2> <p><a class="reference internal" href="#queue.SimpleQueue" title="queue.SimpleQueue"><code>SimpleQueue</code></a> objects provide the public methods described below.</p> <dl class="py method"> <dt class="sig sig-object py" id="queue.SimpleQueue.qsize">
+<code>SimpleQueue.qsize()</code> </dt> <dd>
+<p>Return the approximate size of the queue. Note, qsize() &gt; 0 doesn’t guarantee that a subsequent get() will not block.</p> </dd>
+</dl> <dl class="py method"> <dt class="sig sig-object py" id="queue.SimpleQueue.empty">
+<code>SimpleQueue.empty()</code> </dt> <dd>
+<p>Return <code>True</code> if the queue is empty, <code>False</code> otherwise. If empty() returns <code>False</code> it doesn’t guarantee that a subsequent call to get() will not block.</p> </dd>
+</dl> <dl class="py method"> <dt class="sig sig-object py" id="queue.SimpleQueue.put">
+<code>SimpleQueue.put(item, block=True, timeout=None)</code> </dt> <dd>
+<p>Put <em>item</em> 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 <em>block</em> and <em>timeout</em> are ignored and only provided for compatibility with <a class="reference internal" href="#queue.Queue.put" title="queue.Queue.put"><code>Queue.put()</code></a>.</p> <div class="impl-detail compound"> <p><strong>CPython implementation detail:</strong> This method has a C implementation which is reentrant. That is, a <code>put()</code> or <code>get()</code> call can be interrupted by another <code>put()</code> call in the same thread without deadlocking or corrupting internal state inside the queue. This makes it appropriate for use in destructors such as <code>__del__</code> methods or <a class="reference internal" href="weakref#module-weakref" title="weakref: Support for weak references and weak dictionaries."><code>weakref</code></a> callbacks.</p> </div> </dd>
+</dl> <dl class="py method"> <dt class="sig sig-object py" id="queue.SimpleQueue.put_nowait">
+<code>SimpleQueue.put_nowait(item)</code> </dt> <dd>
+<p>Equivalent to <code>put(item, block=False)</code>, provided for compatibility with <a class="reference internal" href="#queue.Queue.put_nowait" title="queue.Queue.put_nowait"><code>Queue.put_nowait()</code></a>.</p> </dd>
+</dl> <dl class="py method"> <dt class="sig sig-object py" id="queue.SimpleQueue.get">
+<code>SimpleQueue.get(block=True, timeout=None)</code> </dt> <dd>
+<p>Remove and return an item from the queue. If optional args <em>block</em> is true and <em>timeout</em> is <code>None</code> (the default), block if necessary until an item is available. If <em>timeout</em> is a positive number, it blocks at most <em>timeout</em> seconds and raises the <a class="reference internal" href="#queue.Empty" title="queue.Empty"><code>Empty</code></a> exception if no item was available within that time. Otherwise (<em>block</em> is false), return an item if one is immediately available, else raise the <a class="reference internal" href="#queue.Empty" title="queue.Empty"><code>Empty</code></a> exception (<em>timeout</em> is ignored in that case).</p> </dd>
+</dl> <dl class="py method"> <dt class="sig sig-object py" id="queue.SimpleQueue.get_nowait">
+<code>SimpleQueue.get_nowait()</code> </dt> <dd>
+<p>Equivalent to <code>get(False)</code>.</p> </dd>
+</dl> <div class="admonition seealso"> <p class="admonition-title">See also</p> <dl class="simple"> <dt>
+<code>Class</code> <a class="reference internal" href="multiprocessing#multiprocessing.Queue" title="multiprocessing.Queue"><code>multiprocessing.Queue</code></a>
+</dt>
+<dd>
+<p>A queue class for use in a multi-processing (rather than multi-threading) context.</p> </dd> </dl> <p><a class="reference internal" href="collections#collections.deque" title="collections.deque"><code>collections.deque</code></a> is an alternative implementation of unbounded queues with fast atomic <a class="reference internal" href="collections#collections.deque.append" title="collections.deque.append"><code>append()</code></a> and <a class="reference internal" href="collections#collections.deque.popleft" title="collections.deque.popleft"><code>popleft()</code></a> operations that do not require locking and also support indexing.</p> </div> </section> <div class="_attribution">
+ <p class="_attribution-p">
+ &copy; 2001&ndash;2023 Python Software Foundation<br>Licensed under the PSF License.<br>
+ <a href="https://docs.python.org/3.12/library/queue.html" class="_attribution-link">https://docs.python.org/3.12/library/queue.html</a>
+ </p>
+</div>