blob: 716cf09dc99eae9a39cdc43f17428fa8eb9f9cff [file] [log] [blame]
Yury Selivanov7372c3b2018-09-14 15:11:24 -07001.. currentmodule:: asyncio
2
3
Yury Selivanov394374e2018-09-17 15:35:24 -04004====================
5High-level API Index
6====================
Yury Selivanov7372c3b2018-09-14 15:11:24 -07007
8This page lists all high-level async/await enabled asyncio APIs.
9
10
11Tasks
12=====
13
14Utilities to run asyncio programs, create Tasks, and
15await on multiple things with timeouts.
16
17.. list-table::
Yury Selivanov805e27e2018-09-14 16:57:11 -070018 :widths: 50 50
Yury Selivanov394374e2018-09-17 15:35:24 -040019 :class: full-width-table
Yury Selivanov7372c3b2018-09-14 15:11:24 -070020
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 Selivanov394374e2018-09-17 15:35:24 -040040 - Monitor for completion.
Yury Selivanov7372c3b2018-09-14 15:11:24 -070041
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
Yury Selivanov394374e2018-09-17 15:35:24 -040051 * - :func:`run_coroutine_threadsafe`
52 - Schedule a coroutine from another OS thread.
53
54 * - ``for in`` :func:`as_completed`
55 - Monitor for completion with a ``for`` loop.
56
Yury Selivanov7372c3b2018-09-14 15:11:24 -070057
58.. rubric:: Examples
59
60* :ref:`Using asyncio.gather() to run things in parallel
61 <asyncio_example_gather>`.
62
63* :ref:`Using asyncio.wait_for() to enforce a timeout
64 <asyncio_example_waitfor>`.
65
66* :ref:`Cancellation <asyncio_example_task_cancel>`.
67
68* :ref:`Using asyncio.sleep() <asyncio_example_sleep>`.
69
70* See also the main :ref:`Tasks documentation page <coroutine>`.
71
72
73Queues
74======
75
76Queues should be used to distribute work amongst multiple asyncio Tasks,
77implement connection pools, and pub/sub patterns.
78
79
80.. list-table::
Yury Selivanov805e27e2018-09-14 16:57:11 -070081 :widths: 50 50
Yury Selivanov394374e2018-09-17 15:35:24 -040082 :class: full-width-table
Yury Selivanov7372c3b2018-09-14 15:11:24 -070083
84 * - :class:`Queue`
85 - A FIFO queue.
86
87 * - :class:`PriorityQueue`
88 - A priority queue.
89
90 * - :class:`LifoQueue`
91 - A LIFO queue.
92
93
94.. rubric:: Examples
95
96* :ref:`Using asyncio.Queue to distribute workload between several
97 Tasks <asyncio_example_queue_dist>`.
98
99* See also the :ref:`Queues documentation page <asyncio-queues>`.
100
101
102Subprocesses
103============
104
105Utilities to spawn subprocesses and run shell commands.
106
107.. list-table::
Yury Selivanov805e27e2018-09-14 16:57:11 -0700108 :widths: 50 50
Yury Selivanov394374e2018-09-17 15:35:24 -0400109 :class: full-width-table
Yury Selivanov7372c3b2018-09-14 15:11:24 -0700110
111 * - ``await`` :func:`create_subprocess_exec`
112 - Create a subprocess.
113
114 * - ``await`` :func:`create_subprocess_shell`
115 - Run a shell command.
116
117
118.. rubric:: Examples
119
120* :ref:`Executing a shell command <asyncio_example_subprocess_shell>`.
121
122* See also the :ref:`subprocess APIs <asyncio-subprocess>`
123 documentation.
124
125
126Streams
127=======
128
129High-level APIs to work with network IO.
130
131.. list-table::
Yury Selivanov805e27e2018-09-14 16:57:11 -0700132 :widths: 50 50
Yury Selivanov394374e2018-09-17 15:35:24 -0400133 :class: full-width-table
Yury Selivanov7372c3b2018-09-14 15:11:24 -0700134
Xtreakd31b3152019-09-13 11:52:38 +0100135 * - ``await`` :func:`connect`
136 - Establish a TCP connection to send and receive data.
137
Yury Selivanov7372c3b2018-09-14 15:11:24 -0700138 * - ``await`` :func:`open_connection`
Xtreakd31b3152019-09-13 11:52:38 +0100139 - Establish a TCP connection. (Deprecated in favor of :func:`connect`)
140
141 * - ``await`` :func:`connect_unix`
142 - Establish a Unix socket connection to send and receive data.
Yury Selivanov7372c3b2018-09-14 15:11:24 -0700143
144 * - ``await`` :func:`open_unix_connection`
Xtreakd31b3152019-09-13 11:52:38 +0100145 - Establish a Unix socket connection. (Deprecated in favor of :func:`connect_unix`)
Yury Selivanov7372c3b2018-09-14 15:11:24 -0700146
Xtreakd31b3152019-09-13 11:52:38 +0100147 * - :class:`StreamServer`
Yury Selivanov7372c3b2018-09-14 15:11:24 -0700148 - Start a TCP server.
149
Xtreakd31b3152019-09-13 11:52:38 +0100150 * - ``await`` :func:`start_server`
151 - Start a TCP server. (Deprecated in favor of :class:`StreamServer`)
152
153 * - :class:`UnixStreamServer`
Yury Selivanov7372c3b2018-09-14 15:11:24 -0700154 - Start a Unix socket server.
155
Xtreakd31b3152019-09-13 11:52:38 +0100156 * - ``await`` :func:`start_unix_server`
157 - Start a Unix socket server. (Deprecated in favor of :class:`UnixStreamServer`)
158
159 * - :func:`connect_read_pipe`
160 - Establish a connection to :term:`file-like object <file object>` *pipe*
161 to receive data.
162
163 * - :func:`connect_write_pipe`
164 - Establish a connection to :term:`file-like object <file object>` *pipe*
165 to send data.
166
167 * - :class:`Stream`
168 - Stream is a single object combining APIs of :class:`StreamReader` and
169 :class:`StreamWriter`.
170
Yury Selivanov7372c3b2018-09-14 15:11:24 -0700171 * - :class:`StreamReader`
Xtreakd31b3152019-09-13 11:52:38 +0100172 - High-level async/await object to receive network data. (Deprecated in favor of :class:`Stream`)
Yury Selivanov7372c3b2018-09-14 15:11:24 -0700173
174 * - :class:`StreamWriter`
Xtreakd31b3152019-09-13 11:52:38 +0100175 - High-level async/await object to send network data. (Deprecated in favor of :class:`Stream`)
Yury Selivanov7372c3b2018-09-14 15:11:24 -0700176
177
178.. rubric:: Examples
179
180* :ref:`Example TCP client <asyncio_example_stream>`.
181
182* See also the :ref:`streams APIs <asyncio-streams>`
183 documentation.
184
185
186Synchronization
187===============
188
189Threading-like synchronization primitives that can be used in Tasks.
190
191.. list-table::
Yury Selivanov805e27e2018-09-14 16:57:11 -0700192 :widths: 50 50
Yury Selivanov394374e2018-09-17 15:35:24 -0400193 :class: full-width-table
Yury Selivanov7372c3b2018-09-14 15:11:24 -0700194
195 * - :class:`Lock`
196 - A mutex lock.
197
198 * - :class:`Event`
199 - An event object.
200
201 * - :class:`Condition`
202 - A condition object.
203
204 * - :class:`Semaphore`
205 - A semaphore.
206
207 * - :class:`BoundedSemaphore`
208 - A bounded semaphore.
209
210
211.. rubric:: Examples
212
213* :ref:`Using asyncio.Event <asyncio_example_sync_event>`.
214
215* See also the documentation of asyncio
216 :ref:`synchronization primitives <asyncio-sync>`.
217
218
219Exceptions
220==========
221
222.. list-table::
Yury Selivanov805e27e2018-09-14 16:57:11 -0700223 :widths: 50 50
Yury Selivanov394374e2018-09-17 15:35:24 -0400224 :class: full-width-table
Yury Selivanov7372c3b2018-09-14 15:11:24 -0700225
226
227 * - :exc:`asyncio.TimeoutError`
228 - Raised on timeout by functions like :func:`wait_for`.
229 Keep in mind that ``asyncio.TimeoutError`` is **unrelated**
230 to the built-in :exc:`TimeoutError` exception.
231
232 * - :exc:`asyncio.CancelledError`
233 - Raised when a Task is cancelled. See also :meth:`Task.cancel`.
234
235
236.. rubric:: Examples
237
238* :ref:`Handling CancelledError to run code on cancellation request
239 <asyncio_example_task_cancel>`.
240
241* See also the full list of
242 :ref:`asyncio-specific exceptions <asyncio-exceptions>`.