Victor Stinner | 615a58e | 2015-02-25 13:55:43 +0100 | [diff] [blame] | 1 | .. currentmodule:: asyncio |
| 2 | |
Yury Selivanov | 6c73164 | 2018-09-14 14:57:39 -0700 | [diff] [blame] | 3 | .. _asyncio-queues: |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 4 | |
| 5 | ====== |
Victor Stinner | 615a58e | 2015-02-25 13:55:43 +0100 | [diff] [blame] | 6 | Queues |
| 7 | ====== |
| 8 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 9 | asyncio queues are designed to be similar to classes of the |
| 10 | :mod:`queue` module. Although asyncio queues are not thread-safe, |
| 11 | they are designed to be used specifically in async/await code. |
lf | 627d2c8 | 2017-07-25 17:03:51 -0600 | [diff] [blame] | 12 | |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 13 | Note that methods of asyncio queues don't have a *timeout* parameter; |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 14 | use :func:`asyncio.wait_for` function to do queue operations with a |
| 15 | timeout. |
Victor Stinner | 615a58e | 2015-02-25 13:55:43 +0100 | [diff] [blame] | 16 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 17 | See also the `Examples`_ section below. |
Victor Stinner | 615a58e | 2015-02-25 13:55:43 +0100 | [diff] [blame] | 18 | |
| 19 | Queue |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 20 | ===== |
Victor Stinner | 615a58e | 2015-02-25 13:55:43 +0100 | [diff] [blame] | 21 | |
| 22 | .. class:: Queue(maxsize=0, \*, loop=None) |
| 23 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 24 | A first in, first out (FIFO) queue. |
Victor Stinner | 615a58e | 2015-02-25 13:55:43 +0100 | [diff] [blame] | 25 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 26 | If *maxsize* is less than or equal to zero, the queue size is |
| 27 | infinite. If it is an integer greater than ``0``, then |
| 28 | ``await put()`` blocks when the queue reaches *maxsize* |
| 29 | until an item is removed by :meth:`get`. |
Victor Stinner | 615a58e | 2015-02-25 13:55:43 +0100 | [diff] [blame] | 30 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 31 | Unlike the standard library threading :mod:`queue`, the size of |
| 32 | the queue is always known and can be returned by calling the |
| 33 | :meth:`qsize` method. |
Victor Stinner | 615a58e | 2015-02-25 13:55:43 +0100 | [diff] [blame] | 34 | |
Emmanuel Arias | 9008be3 | 2019-09-10 08:46:12 -0300 | [diff] [blame] | 35 | .. deprecated-removed:: 3.8 3.10 |
| 36 | The *loop* parameter. |
| 37 | |
| 38 | |
Victor Stinner | 8370496 | 2015-02-25 14:24:15 +0100 | [diff] [blame] | 39 | This class is :ref:`not thread safe <asyncio-multithreading>`. |
| 40 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 41 | .. attribute:: maxsize |
| 42 | |
| 43 | Number of items allowed in the queue. |
Victor Stinner | 615a58e | 2015-02-25 13:55:43 +0100 | [diff] [blame] | 44 | |
| 45 | .. method:: empty() |
| 46 | |
| 47 | Return ``True`` if the queue is empty, ``False`` otherwise. |
| 48 | |
| 49 | .. method:: full() |
| 50 | |
| 51 | Return ``True`` if there are :attr:`maxsize` items in the queue. |
| 52 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 53 | If the queue was initialized with ``maxsize=0`` (the default), |
| 54 | then :meth:`full()` never returns ``True``. |
Victor Stinner | 615a58e | 2015-02-25 13:55:43 +0100 | [diff] [blame] | 55 | |
| 56 | .. coroutinemethod:: get() |
| 57 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 58 | Remove and return an item from the queue. If queue is empty, |
| 59 | wait until an item is available. |
Victor Stinner | 615a58e | 2015-02-25 13:55:43 +0100 | [diff] [blame] | 60 | |
| 61 | .. method:: get_nowait() |
| 62 | |
Victor Stinner | 615a58e | 2015-02-25 13:55:43 +0100 | [diff] [blame] | 63 | Return an item if one is immediately available, else raise |
| 64 | :exc:`QueueEmpty`. |
| 65 | |
| 66 | .. coroutinemethod:: join() |
| 67 | |
Carol Willing | c9d66f0 | 2018-09-14 10:06:55 -0700 | [diff] [blame] | 68 | Block until all items in the queue have been received and processed. |
Victor Stinner | 615a58e | 2015-02-25 13:55:43 +0100 | [diff] [blame] | 69 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 70 | The count of unfinished tasks goes up whenever an item is added |
Slam | 97e1299 | 2019-01-17 13:52:17 +0200 | [diff] [blame] | 71 | to the queue. The count goes down whenever a consumer coroutine calls |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 72 | :meth:`task_done` to indicate that the item was retrieved and all |
| 73 | work on it is complete. When the count of unfinished tasks drops |
| 74 | to zero, :meth:`join` unblocks. |
Victor Stinner | 615a58e | 2015-02-25 13:55:43 +0100 | [diff] [blame] | 75 | |
| 76 | .. coroutinemethod:: put(item) |
| 77 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 78 | Put an item into the queue. If the queue is full, wait until a |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 79 | free slot is available before adding the item. |
Victor Stinner | 615a58e | 2015-02-25 13:55:43 +0100 | [diff] [blame] | 80 | |
| 81 | .. method:: put_nowait(item) |
| 82 | |
| 83 | Put an item into the queue without blocking. |
| 84 | |
| 85 | If no free slot is immediately available, raise :exc:`QueueFull`. |
| 86 | |
| 87 | .. method:: qsize() |
| 88 | |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 89 | Return the number of items in the queue. |
Victor Stinner | 615a58e | 2015-02-25 13:55:43 +0100 | [diff] [blame] | 90 | |
| 91 | .. method:: task_done() |
| 92 | |
| 93 | Indicate that a formerly enqueued task is complete. |
| 94 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 95 | Used by queue consumers. For each :meth:`~Queue.get` used to |
| 96 | fetch a task, a subsequent call to :meth:`task_done` tells the |
| 97 | queue that the processing on the task is complete. |
Victor Stinner | 615a58e | 2015-02-25 13:55:43 +0100 | [diff] [blame] | 98 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 99 | If a :meth:`join` is currently blocking, it will resume when all |
| 100 | items have been processed (meaning that a :meth:`task_done` |
| 101 | call was received for every item that had been :meth:`~Queue.put` |
| 102 | into the queue). |
Victor Stinner | 615a58e | 2015-02-25 13:55:43 +0100 | [diff] [blame] | 103 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 104 | Raises :exc:`ValueError` if called more times than there were |
| 105 | items placed in the queue. |
Victor Stinner | 615a58e | 2015-02-25 13:55:43 +0100 | [diff] [blame] | 106 | |
| 107 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 108 | Priority Queue |
| 109 | ============== |
Victor Stinner | 615a58e | 2015-02-25 13:55:43 +0100 | [diff] [blame] | 110 | |
| 111 | .. class:: PriorityQueue |
| 112 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 113 | A variant of :class:`Queue`; retrieves entries in priority order |
| 114 | (lowest first). |
Victor Stinner | 615a58e | 2015-02-25 13:55:43 +0100 | [diff] [blame] | 115 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 116 | Entries are typically tuples of the form |
| 117 | ``(priority_number, data)``. |
Victor Stinner | 615a58e | 2015-02-25 13:55:43 +0100 | [diff] [blame] | 118 | |
| 119 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 120 | LIFO Queue |
| 121 | ========== |
Victor Stinner | 615a58e | 2015-02-25 13:55:43 +0100 | [diff] [blame] | 122 | |
| 123 | .. class:: LifoQueue |
| 124 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 125 | A variant of :class:`Queue` that retrieves most recently added |
| 126 | entries first (last in, first out). |
Victor Stinner | 615a58e | 2015-02-25 13:55:43 +0100 | [diff] [blame] | 127 | |
| 128 | |
Victor Stinner | 615a58e | 2015-02-25 13:55:43 +0100 | [diff] [blame] | 129 | Exceptions |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 130 | ========== |
Victor Stinner | 615a58e | 2015-02-25 13:55:43 +0100 | [diff] [blame] | 131 | |
| 132 | .. exception:: QueueEmpty |
| 133 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 134 | This exception is raised when the :meth:`~Queue.get_nowait` method |
| 135 | is called on an empty queue. |
Victor Stinner | 615a58e | 2015-02-25 13:55:43 +0100 | [diff] [blame] | 136 | |
| 137 | |
| 138 | .. exception:: QueueFull |
| 139 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 140 | Exception raised when the :meth:`~Queue.put_nowait` method is called |
| 141 | on a queue that has reached its *maxsize*. |
| 142 | |
| 143 | |
| 144 | Examples |
| 145 | ======== |
| 146 | |
Yury Selivanov | 7372c3b | 2018-09-14 15:11:24 -0700 | [diff] [blame] | 147 | .. _asyncio_example_queue_dist: |
| 148 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 149 | Queues can be used to distribute workload between several |
| 150 | concurrent tasks:: |
| 151 | |
| 152 | import asyncio |
| 153 | import random |
| 154 | import time |
| 155 | |
| 156 | |
| 157 | async def worker(name, queue): |
| 158 | while True: |
| 159 | # Get a "work item" out of the queue. |
| 160 | sleep_for = await queue.get() |
| 161 | |
| 162 | # Sleep for the "sleep_for" seconds. |
| 163 | await asyncio.sleep(sleep_for) |
| 164 | |
| 165 | # Notify the queue that the "work item" has been processed. |
| 166 | queue.task_done() |
| 167 | |
| 168 | print(f'{name} has slept for {sleep_for:.2f} seconds') |
| 169 | |
| 170 | |
| 171 | async def main(): |
| 172 | # Create a queue that we will use to store our "workload". |
| 173 | queue = asyncio.Queue() |
| 174 | |
| 175 | # Generate random timings and put them into the queue. |
| 176 | total_sleep_time = 0 |
| 177 | for _ in range(20): |
| 178 | sleep_for = random.uniform(0.05, 1.0) |
| 179 | total_sleep_time += sleep_for |
| 180 | queue.put_nowait(sleep_for) |
| 181 | |
| 182 | # Create three worker tasks to process the queue concurrently. |
| 183 | tasks = [] |
| 184 | for i in range(3): |
| 185 | task = asyncio.create_task(worker(f'worker-{i}', queue)) |
| 186 | tasks.append(task) |
| 187 | |
| 188 | # Wait until the queue is fully processed. |
| 189 | started_at = time.monotonic() |
| 190 | await queue.join() |
| 191 | total_slept_for = time.monotonic() - started_at |
| 192 | |
| 193 | # Cancel our worker tasks. |
| 194 | for task in tasks: |
| 195 | task.cancel() |
| 196 | # Wait until all worker tasks are cancelled. |
| 197 | await asyncio.gather(*tasks, return_exceptions=True) |
| 198 | |
| 199 | print('====') |
| 200 | print(f'3 workers slept in parallel for {total_slept_for:.2f} seconds') |
| 201 | print(f'total expected sleep time: {total_sleep_time:.2f} seconds') |
| 202 | |
| 203 | |
| 204 | asyncio.run(main()) |