17db96d56Sopenharmony_ci:mod:`queue` --- A synchronized queue class 27db96d56Sopenharmony_ci=========================================== 37db96d56Sopenharmony_ci 47db96d56Sopenharmony_ci.. module:: queue 57db96d56Sopenharmony_ci :synopsis: A synchronized queue class. 67db96d56Sopenharmony_ci 77db96d56Sopenharmony_ci**Source code:** :source:`Lib/queue.py` 87db96d56Sopenharmony_ci 97db96d56Sopenharmony_ci-------------- 107db96d56Sopenharmony_ci 117db96d56Sopenharmony_ciThe :mod:`queue` module implements multi-producer, multi-consumer queues. 127db96d56Sopenharmony_ciIt is especially useful in threaded programming when information must be 137db96d56Sopenharmony_ciexchanged safely between multiple threads. The :class:`Queue` class in this 147db96d56Sopenharmony_cimodule implements all the required locking semantics. 157db96d56Sopenharmony_ci 167db96d56Sopenharmony_ciThe module implements three types of queue, which differ only in the order in 177db96d56Sopenharmony_ciwhich the entries are retrieved. In a :abbr:`FIFO (first-in, first-out)` 187db96d56Sopenharmony_ciqueue, the first tasks added are the first retrieved. In a 197db96d56Sopenharmony_ci:abbr:`LIFO (last-in, first-out)` queue, the most recently added entry is 207db96d56Sopenharmony_cithe first retrieved (operating like a stack). With a priority queue, 217db96d56Sopenharmony_cithe entries are kept sorted (using the :mod:`heapq` module) and the 227db96d56Sopenharmony_cilowest valued entry is retrieved first. 237db96d56Sopenharmony_ci 247db96d56Sopenharmony_ciInternally, those three types of queues use locks to temporarily block 257db96d56Sopenharmony_cicompeting threads; however, they are not designed to handle reentrancy 267db96d56Sopenharmony_ciwithin a thread. 277db96d56Sopenharmony_ci 287db96d56Sopenharmony_ciIn addition, the module implements a "simple" 297db96d56Sopenharmony_ci:abbr:`FIFO (first-in, first-out)` queue type, :class:`SimpleQueue`, whose 307db96d56Sopenharmony_cispecific implementation provides additional guarantees 317db96d56Sopenharmony_ciin exchange for the smaller functionality. 327db96d56Sopenharmony_ci 337db96d56Sopenharmony_ciThe :mod:`queue` module defines the following classes and exceptions: 347db96d56Sopenharmony_ci 357db96d56Sopenharmony_ci.. class:: Queue(maxsize=0) 367db96d56Sopenharmony_ci 377db96d56Sopenharmony_ci Constructor for a :abbr:`FIFO (first-in, first-out)` queue. *maxsize* is 387db96d56Sopenharmony_ci an integer that sets the upperbound 397db96d56Sopenharmony_ci limit on the number of items that can be placed in the queue. Insertion will 407db96d56Sopenharmony_ci block once this size has been reached, until queue items are consumed. If 417db96d56Sopenharmony_ci *maxsize* is less than or equal to zero, the queue size is infinite. 427db96d56Sopenharmony_ci 437db96d56Sopenharmony_ci.. class:: LifoQueue(maxsize=0) 447db96d56Sopenharmony_ci 457db96d56Sopenharmony_ci Constructor for a :abbr:`LIFO (last-in, first-out)` queue. *maxsize* is 467db96d56Sopenharmony_ci an integer that sets the upperbound 477db96d56Sopenharmony_ci limit on the number of items that can be placed in the queue. Insertion will 487db96d56Sopenharmony_ci block once this size has been reached, until queue items are consumed. If 497db96d56Sopenharmony_ci *maxsize* is less than or equal to zero, the queue size is infinite. 507db96d56Sopenharmony_ci 517db96d56Sopenharmony_ci 527db96d56Sopenharmony_ci.. class:: PriorityQueue(maxsize=0) 537db96d56Sopenharmony_ci 547db96d56Sopenharmony_ci Constructor for a priority queue. *maxsize* is an integer that sets the upperbound 557db96d56Sopenharmony_ci limit on the number of items that can be placed in the queue. Insertion will 567db96d56Sopenharmony_ci block once this size has been reached, until queue items are consumed. If 577db96d56Sopenharmony_ci *maxsize* is less than or equal to zero, the queue size is infinite. 587db96d56Sopenharmony_ci 597db96d56Sopenharmony_ci The lowest valued entries are retrieved first (the lowest valued entry is the 607db96d56Sopenharmony_ci one that would be returned by ``min(entries)``). A typical pattern for 617db96d56Sopenharmony_ci entries is a tuple in the form: ``(priority_number, data)``. 627db96d56Sopenharmony_ci 637db96d56Sopenharmony_ci If the *data* elements are not comparable, the data can be wrapped in a class 647db96d56Sopenharmony_ci that ignores the data item and only compares the priority number:: 657db96d56Sopenharmony_ci 667db96d56Sopenharmony_ci from dataclasses import dataclass, field 677db96d56Sopenharmony_ci from typing import Any 687db96d56Sopenharmony_ci 697db96d56Sopenharmony_ci @dataclass(order=True) 707db96d56Sopenharmony_ci class PrioritizedItem: 717db96d56Sopenharmony_ci priority: int 727db96d56Sopenharmony_ci item: Any=field(compare=False) 737db96d56Sopenharmony_ci 747db96d56Sopenharmony_ci.. class:: SimpleQueue() 757db96d56Sopenharmony_ci 767db96d56Sopenharmony_ci Constructor for an unbounded :abbr:`FIFO (first-in, first-out)` queue. 777db96d56Sopenharmony_ci Simple queues lack advanced functionality such as task tracking. 787db96d56Sopenharmony_ci 797db96d56Sopenharmony_ci .. versionadded:: 3.7 807db96d56Sopenharmony_ci 817db96d56Sopenharmony_ci 827db96d56Sopenharmony_ci.. exception:: Empty 837db96d56Sopenharmony_ci 847db96d56Sopenharmony_ci Exception raised when non-blocking :meth:`~Queue.get` (or 857db96d56Sopenharmony_ci :meth:`~Queue.get_nowait`) is called 867db96d56Sopenharmony_ci on a :class:`Queue` object which is empty. 877db96d56Sopenharmony_ci 887db96d56Sopenharmony_ci 897db96d56Sopenharmony_ci.. exception:: Full 907db96d56Sopenharmony_ci 917db96d56Sopenharmony_ci Exception raised when non-blocking :meth:`~Queue.put` (or 927db96d56Sopenharmony_ci :meth:`~Queue.put_nowait`) is called 937db96d56Sopenharmony_ci on a :class:`Queue` object which is full. 947db96d56Sopenharmony_ci 957db96d56Sopenharmony_ci 967db96d56Sopenharmony_ci.. _queueobjects: 977db96d56Sopenharmony_ci 987db96d56Sopenharmony_ciQueue Objects 997db96d56Sopenharmony_ci------------- 1007db96d56Sopenharmony_ci 1017db96d56Sopenharmony_ciQueue objects (:class:`Queue`, :class:`LifoQueue`, or :class:`PriorityQueue`) 1027db96d56Sopenharmony_ciprovide the public methods described below. 1037db96d56Sopenharmony_ci 1047db96d56Sopenharmony_ci 1057db96d56Sopenharmony_ci.. method:: Queue.qsize() 1067db96d56Sopenharmony_ci 1077db96d56Sopenharmony_ci Return the approximate size of the queue. Note, qsize() > 0 doesn't 1087db96d56Sopenharmony_ci guarantee that a subsequent get() will not block, nor will qsize() < maxsize 1097db96d56Sopenharmony_ci guarantee that put() will not block. 1107db96d56Sopenharmony_ci 1117db96d56Sopenharmony_ci 1127db96d56Sopenharmony_ci.. method:: Queue.empty() 1137db96d56Sopenharmony_ci 1147db96d56Sopenharmony_ci Return ``True`` if the queue is empty, ``False`` otherwise. If empty() 1157db96d56Sopenharmony_ci returns ``True`` it doesn't guarantee that a subsequent call to put() 1167db96d56Sopenharmony_ci will not block. Similarly, if empty() returns ``False`` it doesn't 1177db96d56Sopenharmony_ci guarantee that a subsequent call to get() will not block. 1187db96d56Sopenharmony_ci 1197db96d56Sopenharmony_ci 1207db96d56Sopenharmony_ci.. method:: Queue.full() 1217db96d56Sopenharmony_ci 1227db96d56Sopenharmony_ci Return ``True`` if the queue is full, ``False`` otherwise. If full() 1237db96d56Sopenharmony_ci returns ``True`` it doesn't guarantee that a subsequent call to get() 1247db96d56Sopenharmony_ci will not block. Similarly, if full() returns ``False`` it doesn't 1257db96d56Sopenharmony_ci guarantee that a subsequent call to put() will not block. 1267db96d56Sopenharmony_ci 1277db96d56Sopenharmony_ci 1287db96d56Sopenharmony_ci.. method:: Queue.put(item, block=True, timeout=None) 1297db96d56Sopenharmony_ci 1307db96d56Sopenharmony_ci Put *item* into the queue. If optional args *block* is true and *timeout* is 1317db96d56Sopenharmony_ci ``None`` (the default), block if necessary until a free slot is available. If 1327db96d56Sopenharmony_ci *timeout* is a positive number, it blocks at most *timeout* seconds and raises 1337db96d56Sopenharmony_ci the :exc:`Full` exception if no free slot was available within that time. 1347db96d56Sopenharmony_ci Otherwise (*block* is false), put an item on the queue if a free slot is 1357db96d56Sopenharmony_ci immediately available, else raise the :exc:`Full` exception (*timeout* is 1367db96d56Sopenharmony_ci ignored in that case). 1377db96d56Sopenharmony_ci 1387db96d56Sopenharmony_ci 1397db96d56Sopenharmony_ci.. method:: Queue.put_nowait(item) 1407db96d56Sopenharmony_ci 1417db96d56Sopenharmony_ci Equivalent to ``put(item, block=False)``. 1427db96d56Sopenharmony_ci 1437db96d56Sopenharmony_ci 1447db96d56Sopenharmony_ci.. method:: Queue.get(block=True, timeout=None) 1457db96d56Sopenharmony_ci 1467db96d56Sopenharmony_ci Remove and return an item from the queue. If optional args *block* is true and 1477db96d56Sopenharmony_ci *timeout* is ``None`` (the default), block if necessary until an item is available. 1487db96d56Sopenharmony_ci If *timeout* is a positive number, it blocks at most *timeout* seconds and 1497db96d56Sopenharmony_ci raises the :exc:`Empty` exception if no item was available within that time. 1507db96d56Sopenharmony_ci Otherwise (*block* is false), return an item if one is immediately available, 1517db96d56Sopenharmony_ci else raise the :exc:`Empty` exception (*timeout* is ignored in that case). 1527db96d56Sopenharmony_ci 1537db96d56Sopenharmony_ci Prior to 3.0 on POSIX systems, and for all versions on Windows, if 1547db96d56Sopenharmony_ci *block* is true and *timeout* is ``None``, this operation goes into 1557db96d56Sopenharmony_ci an uninterruptible wait on an underlying lock. This means that no exceptions 1567db96d56Sopenharmony_ci can occur, and in particular a SIGINT will not trigger a :exc:`KeyboardInterrupt`. 1577db96d56Sopenharmony_ci 1587db96d56Sopenharmony_ci 1597db96d56Sopenharmony_ci.. method:: Queue.get_nowait() 1607db96d56Sopenharmony_ci 1617db96d56Sopenharmony_ci Equivalent to ``get(False)``. 1627db96d56Sopenharmony_ci 1637db96d56Sopenharmony_ciTwo methods are offered to support tracking whether enqueued tasks have been 1647db96d56Sopenharmony_cifully processed by daemon consumer threads. 1657db96d56Sopenharmony_ci 1667db96d56Sopenharmony_ci 1677db96d56Sopenharmony_ci.. method:: Queue.task_done() 1687db96d56Sopenharmony_ci 1697db96d56Sopenharmony_ci Indicate that a formerly enqueued task is complete. Used by queue consumer 1707db96d56Sopenharmony_ci threads. For each :meth:`get` used to fetch a task, a subsequent call to 1717db96d56Sopenharmony_ci :meth:`task_done` tells the queue that the processing on the task is complete. 1727db96d56Sopenharmony_ci 1737db96d56Sopenharmony_ci If a :meth:`join` is currently blocking, it will resume when all items have been 1747db96d56Sopenharmony_ci processed (meaning that a :meth:`task_done` call was received for every item 1757db96d56Sopenharmony_ci that had been :meth:`put` into the queue). 1767db96d56Sopenharmony_ci 1777db96d56Sopenharmony_ci Raises a :exc:`ValueError` if called more times than there were items placed in 1787db96d56Sopenharmony_ci the queue. 1797db96d56Sopenharmony_ci 1807db96d56Sopenharmony_ci 1817db96d56Sopenharmony_ci.. method:: Queue.join() 1827db96d56Sopenharmony_ci 1837db96d56Sopenharmony_ci Blocks until all items in the queue have been gotten and processed. 1847db96d56Sopenharmony_ci 1857db96d56Sopenharmony_ci The count of unfinished tasks goes up whenever an item is added to the queue. 1867db96d56Sopenharmony_ci The count goes down whenever a consumer thread calls :meth:`task_done` to 1877db96d56Sopenharmony_ci indicate that the item was retrieved and all work on it is complete. When the 1887db96d56Sopenharmony_ci count of unfinished tasks drops to zero, :meth:`join` unblocks. 1897db96d56Sopenharmony_ci 1907db96d56Sopenharmony_ci 1917db96d56Sopenharmony_ciExample of how to wait for enqueued tasks to be completed:: 1927db96d56Sopenharmony_ci 1937db96d56Sopenharmony_ci import threading 1947db96d56Sopenharmony_ci import queue 1957db96d56Sopenharmony_ci 1967db96d56Sopenharmony_ci q = queue.Queue() 1977db96d56Sopenharmony_ci 1987db96d56Sopenharmony_ci def worker(): 1997db96d56Sopenharmony_ci while True: 2007db96d56Sopenharmony_ci item = q.get() 2017db96d56Sopenharmony_ci print(f'Working on {item}') 2027db96d56Sopenharmony_ci print(f'Finished {item}') 2037db96d56Sopenharmony_ci q.task_done() 2047db96d56Sopenharmony_ci 2057db96d56Sopenharmony_ci # Turn-on the worker thread. 2067db96d56Sopenharmony_ci threading.Thread(target=worker, daemon=True).start() 2077db96d56Sopenharmony_ci 2087db96d56Sopenharmony_ci # Send thirty task requests to the worker. 2097db96d56Sopenharmony_ci for item in range(30): 2107db96d56Sopenharmony_ci q.put(item) 2117db96d56Sopenharmony_ci 2127db96d56Sopenharmony_ci # Block until all tasks are done. 2137db96d56Sopenharmony_ci q.join() 2147db96d56Sopenharmony_ci print('All work completed') 2157db96d56Sopenharmony_ci 2167db96d56Sopenharmony_ci 2177db96d56Sopenharmony_ciSimpleQueue Objects 2187db96d56Sopenharmony_ci------------------- 2197db96d56Sopenharmony_ci 2207db96d56Sopenharmony_ci:class:`SimpleQueue` objects provide the public methods described below. 2217db96d56Sopenharmony_ci 2227db96d56Sopenharmony_ci.. method:: SimpleQueue.qsize() 2237db96d56Sopenharmony_ci 2247db96d56Sopenharmony_ci Return the approximate size of the queue. Note, qsize() > 0 doesn't 2257db96d56Sopenharmony_ci guarantee that a subsequent get() will not block. 2267db96d56Sopenharmony_ci 2277db96d56Sopenharmony_ci 2287db96d56Sopenharmony_ci.. method:: SimpleQueue.empty() 2297db96d56Sopenharmony_ci 2307db96d56Sopenharmony_ci Return ``True`` if the queue is empty, ``False`` otherwise. If empty() 2317db96d56Sopenharmony_ci returns ``False`` it doesn't guarantee that a subsequent call to get() 2327db96d56Sopenharmony_ci will not block. 2337db96d56Sopenharmony_ci 2347db96d56Sopenharmony_ci 2357db96d56Sopenharmony_ci.. method:: SimpleQueue.put(item, block=True, timeout=None) 2367db96d56Sopenharmony_ci 2377db96d56Sopenharmony_ci Put *item* into the queue. The method never blocks and always succeeds 2387db96d56Sopenharmony_ci (except for potential low-level errors such as failure to allocate memory). 2397db96d56Sopenharmony_ci The optional args *block* and *timeout* are ignored and only provided 2407db96d56Sopenharmony_ci for compatibility with :meth:`Queue.put`. 2417db96d56Sopenharmony_ci 2427db96d56Sopenharmony_ci .. impl-detail:: 2437db96d56Sopenharmony_ci This method has a C implementation which is reentrant. That is, a 2447db96d56Sopenharmony_ci ``put()`` or ``get()`` call can be interrupted by another ``put()`` 2457db96d56Sopenharmony_ci call in the same thread without deadlocking or corrupting internal 2467db96d56Sopenharmony_ci state inside the queue. This makes it appropriate for use in 2477db96d56Sopenharmony_ci destructors such as ``__del__`` methods or :mod:`weakref` callbacks. 2487db96d56Sopenharmony_ci 2497db96d56Sopenharmony_ci 2507db96d56Sopenharmony_ci.. method:: SimpleQueue.put_nowait(item) 2517db96d56Sopenharmony_ci 2527db96d56Sopenharmony_ci Equivalent to ``put(item, block=False)``, provided for compatibility with 2537db96d56Sopenharmony_ci :meth:`Queue.put_nowait`. 2547db96d56Sopenharmony_ci 2557db96d56Sopenharmony_ci 2567db96d56Sopenharmony_ci.. method:: SimpleQueue.get(block=True, timeout=None) 2577db96d56Sopenharmony_ci 2587db96d56Sopenharmony_ci Remove and return an item from the queue. If optional args *block* is true and 2597db96d56Sopenharmony_ci *timeout* is ``None`` (the default), block if necessary until an item is available. 2607db96d56Sopenharmony_ci If *timeout* is a positive number, it blocks at most *timeout* seconds and 2617db96d56Sopenharmony_ci raises the :exc:`Empty` exception if no item was available within that time. 2627db96d56Sopenharmony_ci Otherwise (*block* is false), return an item if one is immediately available, 2637db96d56Sopenharmony_ci else raise the :exc:`Empty` exception (*timeout* is ignored in that case). 2647db96d56Sopenharmony_ci 2657db96d56Sopenharmony_ci 2667db96d56Sopenharmony_ci.. method:: SimpleQueue.get_nowait() 2677db96d56Sopenharmony_ci 2687db96d56Sopenharmony_ci Equivalent to ``get(False)``. 2697db96d56Sopenharmony_ci 2707db96d56Sopenharmony_ci 2717db96d56Sopenharmony_ci.. seealso:: 2727db96d56Sopenharmony_ci 2737db96d56Sopenharmony_ci Class :class:`multiprocessing.Queue` 2747db96d56Sopenharmony_ci A queue class for use in a multi-processing (rather than multi-threading) 2757db96d56Sopenharmony_ci context. 2767db96d56Sopenharmony_ci 2777db96d56Sopenharmony_ci :class:`collections.deque` is an alternative implementation of unbounded 2787db96d56Sopenharmony_ci queues with fast atomic :meth:`~collections.deque.append` and 2797db96d56Sopenharmony_ci :meth:`~collections.deque.popleft` operations that do not require locking 2807db96d56Sopenharmony_ci and also support indexing. 281