Yury Selivanov | 7372c3b | 2018-09-14 15:11:24 -0700 | [diff] [blame] | 1 | .. currentmodule:: asyncio |
| 2 | |
| 3 | |
Yury Selivanov | 394374e | 2018-09-17 15:35:24 -0400 | [diff] [blame] | 4 | ==================== |
| 5 | High-level API Index |
| 6 | ==================== |
Yury Selivanov | 7372c3b | 2018-09-14 15:11:24 -0700 | [diff] [blame] | 7 | |
| 8 | This page lists all high-level async/await enabled asyncio APIs. |
| 9 | |
| 10 | |
| 11 | Tasks |
| 12 | ===== |
| 13 | |
| 14 | Utilities to run asyncio programs, create Tasks, and |
| 15 | await on multiple things with timeouts. |
| 16 | |
| 17 | .. list-table:: |
Yury Selivanov | 805e27e | 2018-09-14 16:57:11 -0700 | [diff] [blame] | 18 | :widths: 50 50 |
Yury Selivanov | 394374e | 2018-09-17 15:35:24 -0400 | [diff] [blame] | 19 | :class: full-width-table |
Yury Selivanov | 7372c3b | 2018-09-14 15:11:24 -0700 | [diff] [blame] | 20 | |
| 21 | * - :func:`run` |
| 22 | - Create event loop, run a coroutine, close the loop. |
| 23 | |
| 24 | * - :func:`create_task` |
| 25 | - Start an asyncio Task. |
| 26 | |
| 27 | * - ``await`` :func:`sleep` |
| 28 | - Sleep for a number of seconds. |
| 29 | |
| 30 | * - ``await`` :func:`gather` |
| 31 | - Schedule and wait for things concurrently. |
| 32 | |
| 33 | * - ``await`` :func:`wait_for` |
| 34 | - Run with a timeout. |
| 35 | |
| 36 | * - ``await`` :func:`shield` |
| 37 | - Shield from cancellation. |
| 38 | |
| 39 | * - ``await`` :func:`wait` |
Yury Selivanov | 394374e | 2018-09-17 15:35:24 -0400 | [diff] [blame] | 40 | - Monitor for completion. |
Yury Selivanov | 7372c3b | 2018-09-14 15:11:24 -0700 | [diff] [blame] | 41 | |
| 42 | * - :func:`current_task` |
| 43 | - Return the current Task. |
| 44 | |
| 45 | * - :func:`all_tasks` |
| 46 | - Return all tasks for an event loop. |
| 47 | |
| 48 | * - :class:`Task` |
| 49 | - Task object. |
| 50 | |
Kyle Stanley | cc2bbc2 | 2020-05-18 23:03:28 -0400 | [diff] [blame] | 51 | * - :func:`to_thread` |
| 52 | - Asychronously run a function in a separate OS thread. |
| 53 | |
Yury Selivanov | 394374e | 2018-09-17 15:35:24 -0400 | [diff] [blame] | 54 | * - :func:`run_coroutine_threadsafe` |
| 55 | - Schedule a coroutine from another OS thread. |
| 56 | |
| 57 | * - ``for in`` :func:`as_completed` |
| 58 | - Monitor for completion with a ``for`` loop. |
| 59 | |
Yury Selivanov | 7372c3b | 2018-09-14 15:11:24 -0700 | [diff] [blame] | 60 | |
| 61 | .. rubric:: Examples |
| 62 | |
| 63 | * :ref:`Using asyncio.gather() to run things in parallel |
| 64 | <asyncio_example_gather>`. |
| 65 | |
| 66 | * :ref:`Using asyncio.wait_for() to enforce a timeout |
| 67 | <asyncio_example_waitfor>`. |
| 68 | |
| 69 | * :ref:`Cancellation <asyncio_example_task_cancel>`. |
| 70 | |
| 71 | * :ref:`Using asyncio.sleep() <asyncio_example_sleep>`. |
| 72 | |
| 73 | * See also the main :ref:`Tasks documentation page <coroutine>`. |
| 74 | |
| 75 | |
| 76 | Queues |
| 77 | ====== |
| 78 | |
| 79 | Queues should be used to distribute work amongst multiple asyncio Tasks, |
| 80 | implement connection pools, and pub/sub patterns. |
| 81 | |
| 82 | |
| 83 | .. list-table:: |
Yury Selivanov | 805e27e | 2018-09-14 16:57:11 -0700 | [diff] [blame] | 84 | :widths: 50 50 |
Yury Selivanov | 394374e | 2018-09-17 15:35:24 -0400 | [diff] [blame] | 85 | :class: full-width-table |
Yury Selivanov | 7372c3b | 2018-09-14 15:11:24 -0700 | [diff] [blame] | 86 | |
| 87 | * - :class:`Queue` |
| 88 | - A FIFO queue. |
| 89 | |
| 90 | * - :class:`PriorityQueue` |
| 91 | - A priority queue. |
| 92 | |
| 93 | * - :class:`LifoQueue` |
| 94 | - A LIFO queue. |
| 95 | |
| 96 | |
| 97 | .. rubric:: Examples |
| 98 | |
| 99 | * :ref:`Using asyncio.Queue to distribute workload between several |
| 100 | Tasks <asyncio_example_queue_dist>`. |
| 101 | |
| 102 | * See also the :ref:`Queues documentation page <asyncio-queues>`. |
| 103 | |
| 104 | |
| 105 | Subprocesses |
| 106 | ============ |
| 107 | |
| 108 | Utilities to spawn subprocesses and run shell commands. |
| 109 | |
| 110 | .. list-table:: |
Yury Selivanov | 805e27e | 2018-09-14 16:57:11 -0700 | [diff] [blame] | 111 | :widths: 50 50 |
Yury Selivanov | 394374e | 2018-09-17 15:35:24 -0400 | [diff] [blame] | 112 | :class: full-width-table |
Yury Selivanov | 7372c3b | 2018-09-14 15:11:24 -0700 | [diff] [blame] | 113 | |
| 114 | * - ``await`` :func:`create_subprocess_exec` |
| 115 | - Create a subprocess. |
| 116 | |
| 117 | * - ``await`` :func:`create_subprocess_shell` |
| 118 | - Run a shell command. |
| 119 | |
| 120 | |
| 121 | .. rubric:: Examples |
| 122 | |
| 123 | * :ref:`Executing a shell command <asyncio_example_subprocess_shell>`. |
| 124 | |
| 125 | * See also the :ref:`subprocess APIs <asyncio-subprocess>` |
| 126 | documentation. |
| 127 | |
| 128 | |
| 129 | Streams |
| 130 | ======= |
| 131 | |
| 132 | High-level APIs to work with network IO. |
| 133 | |
| 134 | .. list-table:: |
Yury Selivanov | 805e27e | 2018-09-14 16:57:11 -0700 | [diff] [blame] | 135 | :widths: 50 50 |
Yury Selivanov | 394374e | 2018-09-17 15:35:24 -0400 | [diff] [blame] | 136 | :class: full-width-table |
Yury Selivanov | 7372c3b | 2018-09-14 15:11:24 -0700 | [diff] [blame] | 137 | |
| 138 | * - ``await`` :func:`open_connection` |
Yury Selivanov | 6758e6e | 2019-09-29 21:59:55 -0700 | [diff] [blame] | 139 | - Establish a TCP connection. |
Yury Selivanov | 7372c3b | 2018-09-14 15:11:24 -0700 | [diff] [blame] | 140 | |
| 141 | * - ``await`` :func:`open_unix_connection` |
Yury Selivanov | 6758e6e | 2019-09-29 21:59:55 -0700 | [diff] [blame] | 142 | - Establish a Unix socket connection. |
Yury Selivanov | 7372c3b | 2018-09-14 15:11:24 -0700 | [diff] [blame] | 143 | |
Xtreak | d31b315 | 2019-09-13 11:52:38 +0100 | [diff] [blame] | 144 | * - ``await`` :func:`start_server` |
Yury Selivanov | 6758e6e | 2019-09-29 21:59:55 -0700 | [diff] [blame] | 145 | - Start a TCP server. |
Yury Selivanov | 7372c3b | 2018-09-14 15:11:24 -0700 | [diff] [blame] | 146 | |
Xtreak | d31b315 | 2019-09-13 11:52:38 +0100 | [diff] [blame] | 147 | * - ``await`` :func:`start_unix_server` |
Yury Selivanov | 6758e6e | 2019-09-29 21:59:55 -0700 | [diff] [blame] | 148 | - Start a Unix socket server. |
Xtreak | d31b315 | 2019-09-13 11:52:38 +0100 | [diff] [blame] | 149 | |
Yury Selivanov | 7372c3b | 2018-09-14 15:11:24 -0700 | [diff] [blame] | 150 | * - :class:`StreamReader` |
Yury Selivanov | 6758e6e | 2019-09-29 21:59:55 -0700 | [diff] [blame] | 151 | - High-level async/await object to receive network data. |
Yury Selivanov | 7372c3b | 2018-09-14 15:11:24 -0700 | [diff] [blame] | 152 | |
| 153 | * - :class:`StreamWriter` |
Yury Selivanov | 6758e6e | 2019-09-29 21:59:55 -0700 | [diff] [blame] | 154 | - High-level async/await object to send network data. |
Yury Selivanov | 7372c3b | 2018-09-14 15:11:24 -0700 | [diff] [blame] | 155 | |
| 156 | |
| 157 | .. rubric:: Examples |
| 158 | |
| 159 | * :ref:`Example TCP client <asyncio_example_stream>`. |
| 160 | |
| 161 | * See also the :ref:`streams APIs <asyncio-streams>` |
| 162 | documentation. |
| 163 | |
| 164 | |
| 165 | Synchronization |
| 166 | =============== |
| 167 | |
| 168 | Threading-like synchronization primitives that can be used in Tasks. |
| 169 | |
| 170 | .. list-table:: |
Yury Selivanov | 805e27e | 2018-09-14 16:57:11 -0700 | [diff] [blame] | 171 | :widths: 50 50 |
Yury Selivanov | 394374e | 2018-09-17 15:35:24 -0400 | [diff] [blame] | 172 | :class: full-width-table |
Yury Selivanov | 7372c3b | 2018-09-14 15:11:24 -0700 | [diff] [blame] | 173 | |
| 174 | * - :class:`Lock` |
| 175 | - A mutex lock. |
| 176 | |
| 177 | * - :class:`Event` |
| 178 | - An event object. |
| 179 | |
| 180 | * - :class:`Condition` |
| 181 | - A condition object. |
| 182 | |
| 183 | * - :class:`Semaphore` |
| 184 | - A semaphore. |
| 185 | |
| 186 | * - :class:`BoundedSemaphore` |
| 187 | - A bounded semaphore. |
| 188 | |
| 189 | |
| 190 | .. rubric:: Examples |
| 191 | |
| 192 | * :ref:`Using asyncio.Event <asyncio_example_sync_event>`. |
| 193 | |
| 194 | * See also the documentation of asyncio |
| 195 | :ref:`synchronization primitives <asyncio-sync>`. |
| 196 | |
| 197 | |
| 198 | Exceptions |
| 199 | ========== |
| 200 | |
| 201 | .. list-table:: |
Yury Selivanov | 805e27e | 2018-09-14 16:57:11 -0700 | [diff] [blame] | 202 | :widths: 50 50 |
Yury Selivanov | 394374e | 2018-09-17 15:35:24 -0400 | [diff] [blame] | 203 | :class: full-width-table |
Yury Selivanov | 7372c3b | 2018-09-14 15:11:24 -0700 | [diff] [blame] | 204 | |
| 205 | |
| 206 | * - :exc:`asyncio.TimeoutError` |
| 207 | - Raised on timeout by functions like :func:`wait_for`. |
| 208 | Keep in mind that ``asyncio.TimeoutError`` is **unrelated** |
| 209 | to the built-in :exc:`TimeoutError` exception. |
| 210 | |
| 211 | * - :exc:`asyncio.CancelledError` |
| 212 | - Raised when a Task is cancelled. See also :meth:`Task.cancel`. |
| 213 | |
| 214 | |
| 215 | .. rubric:: Examples |
| 216 | |
| 217 | * :ref:`Handling CancelledError to run code on cancellation request |
| 218 | <asyncio_example_task_cancel>`. |
| 219 | |
| 220 | * See also the full list of |
| 221 | :ref:`asyncio-specific exceptions <asyncio-exceptions>`. |