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