Victor Stinner | 615a58e | 2015-02-25 13:55:43 +0100 | [diff] [blame] | 1 | .. currentmodule:: asyncio |
| 2 | |
| 3 | Queues |
| 4 | ====== |
| 5 | |
| 6 | Queues: |
| 7 | |
| 8 | * :class:`Queue` |
| 9 | * :class:`PriorityQueue` |
| 10 | * :class:`LifoQueue` |
| 11 | * :class:`JoinableQueue` |
| 12 | |
| 13 | asyncio queue API was designed to be close to classes of the :mod:`queue` |
| 14 | module (:class:`~queue.Queue`, :class:`~queue.PriorityQueue`, |
| 15 | :class:`~queue.LifoQueue`), but it has no *timeout* parameter. The |
| 16 | :func:`asyncio.wait_for` function can be used to cancel a task after a timeout. |
| 17 | |
| 18 | Queue |
| 19 | ----- |
| 20 | |
| 21 | .. class:: Queue(maxsize=0, \*, loop=None) |
| 22 | |
| 23 | A queue, useful for coordinating producer and consumer coroutines. |
| 24 | |
| 25 | If *maxsize* is less than or equal to zero, the queue size is infinite. If |
| 26 | it is an integer greater than ``0``, then ``yield from put()`` will block |
| 27 | when the queue reaches *maxsize*, until an item is removed by :meth:`get`. |
| 28 | |
| 29 | Unlike the standard library :mod:`queue`, you can reliably know this Queue's |
| 30 | size with :meth:`qsize`, since your single-threaded asyncio application won't |
| 31 | be interrupted between calling :meth:`qsize` and doing an operation on the |
| 32 | Queue. |
| 33 | |
Victor Stinner | 8370496 | 2015-02-25 14:24:15 +0100 | [diff] [blame] | 34 | This class is :ref:`not thread safe <asyncio-multithreading>`. |
| 35 | |
Victor Stinner | 84c717d | 2015-03-10 16:27:54 +0100 | [diff] [blame] | 36 | .. versionchanged:: 3.4.4 |
Victor Stinner | 615a58e | 2015-02-25 13:55:43 +0100 | [diff] [blame] | 37 | New :meth:`join` and :meth:`task_done` methods. |
| 38 | |
| 39 | .. method:: empty() |
| 40 | |
| 41 | Return ``True`` if the queue is empty, ``False`` otherwise. |
| 42 | |
| 43 | .. method:: full() |
| 44 | |
| 45 | Return ``True`` if there are :attr:`maxsize` items in the queue. |
| 46 | |
| 47 | .. note:: |
| 48 | |
| 49 | If the Queue was initialized with ``maxsize=0`` (the default), then |
| 50 | :meth:`full()` is never ``True``. |
| 51 | |
| 52 | .. coroutinemethod:: get() |
| 53 | |
| 54 | Remove and return an item from the queue. If queue is empty, wait until |
| 55 | an item is available. |
| 56 | |
| 57 | This method is a :ref:`coroutine <coroutine>`. |
| 58 | |
| 59 | .. seealso:: |
| 60 | |
| 61 | The :meth:`empty` method. |
| 62 | |
| 63 | .. method:: get_nowait() |
| 64 | |
| 65 | Remove and return an item from the queue. |
| 66 | |
| 67 | Return an item if one is immediately available, else raise |
| 68 | :exc:`QueueEmpty`. |
| 69 | |
| 70 | .. coroutinemethod:: join() |
| 71 | |
| 72 | Block until all items in the queue have been gotten and processed. |
| 73 | |
| 74 | The count of unfinished tasks goes up whenever an item is added to the |
| 75 | queue. The count goes down whenever a consumer thread calls |
| 76 | :meth:`task_done` to indicate that the item was retrieved and all work on |
| 77 | it is complete. When the count of unfinished tasks drops to zero, |
| 78 | :meth:`join` unblocks. |
| 79 | |
| 80 | This method is a :ref:`coroutine <coroutine>`. |
| 81 | |
Victor Stinner | 84c717d | 2015-03-10 16:27:54 +0100 | [diff] [blame] | 82 | .. versionadded:: 3.4.4 |
Victor Stinner | 615a58e | 2015-02-25 13:55:43 +0100 | [diff] [blame] | 83 | |
| 84 | .. coroutinemethod:: put(item) |
| 85 | |
| 86 | Put an item into the queue. If the queue is full, wait until a free slot |
| 87 | is available before adding item. |
| 88 | |
| 89 | This method is a :ref:`coroutine <coroutine>`. |
| 90 | |
| 91 | .. seealso:: |
| 92 | |
| 93 | The :meth:`full` method. |
| 94 | |
| 95 | .. method:: put_nowait(item) |
| 96 | |
| 97 | Put an item into the queue without blocking. |
| 98 | |
| 99 | If no free slot is immediately available, raise :exc:`QueueFull`. |
| 100 | |
| 101 | .. method:: qsize() |
| 102 | |
| 103 | Number of items in the queue. |
| 104 | |
| 105 | .. method:: task_done() |
| 106 | |
| 107 | Indicate that a formerly enqueued task is complete. |
| 108 | |
| 109 | Used by queue consumers. For each :meth:`~Queue.get` used to fetch a task, a |
| 110 | subsequent call to :meth:`task_done` tells the queue that the processing |
| 111 | on the task is complete. |
| 112 | |
| 113 | If a :meth:`join` is currently blocking, it will resume when all items |
| 114 | have been processed (meaning that a :meth:`task_done` call was received |
| 115 | for every item that had been :meth:`~Queue.put` into the queue). |
| 116 | |
| 117 | Raises :exc:`ValueError` if called more times than there were items |
| 118 | placed in the queue. |
| 119 | |
Victor Stinner | 84c717d | 2015-03-10 16:27:54 +0100 | [diff] [blame] | 120 | .. versionadded:: 3.4.4 |
Victor Stinner | 615a58e | 2015-02-25 13:55:43 +0100 | [diff] [blame] | 121 | |
| 122 | .. attribute:: maxsize |
| 123 | |
| 124 | Number of items allowed in the queue. |
| 125 | |
| 126 | |
| 127 | PriorityQueue |
| 128 | ------------- |
| 129 | |
| 130 | .. class:: PriorityQueue |
| 131 | |
| 132 | A subclass of :class:`Queue`; retrieves entries in priority order (lowest |
| 133 | first). |
| 134 | |
| 135 | Entries are typically tuples of the form: (priority number, data). |
| 136 | |
| 137 | |
| 138 | LifoQueue |
| 139 | --------- |
| 140 | |
| 141 | .. class:: LifoQueue |
| 142 | |
| 143 | A subclass of :class:`Queue` that retrieves most recently added entries |
| 144 | first. |
| 145 | |
| 146 | |
| 147 | JoinableQueue |
| 148 | ^^^^^^^^^^^^^ |
| 149 | |
| 150 | .. class:: JoinableQueue |
| 151 | |
| 152 | Deprecated alias for :class:`Queue`. |
| 153 | |
Victor Stinner | 84c717d | 2015-03-10 16:27:54 +0100 | [diff] [blame] | 154 | .. deprecated:: 3.4.4 |
Victor Stinner | 615a58e | 2015-02-25 13:55:43 +0100 | [diff] [blame] | 155 | |
| 156 | |
| 157 | Exceptions |
| 158 | ^^^^^^^^^^ |
| 159 | |
| 160 | .. exception:: QueueEmpty |
| 161 | |
| 162 | Exception raised when the :meth:`~Queue.get_nowait` method is called on a |
| 163 | :class:`Queue` object which is empty. |
| 164 | |
| 165 | |
| 166 | .. exception:: QueueFull |
| 167 | |
| 168 | Exception raised when the :meth:`~Queue.put_nowait` method is called on a |
| 169 | :class:`Queue` object which is full. |