Skip to content

Commit 7372c3b

Browse files
authored
bpo-33649: Add high-level APIs cheat-sheet (GH-9319)
1 parent 6c73164 commit 7372c3b

9 files changed

+229
-6
lines changed

Doc/library/asyncio-api-index.rst

Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
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::
18+
:widths: 30 70
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::
74+
:widths: 30 70
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::
100+
:widths: 30 70
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::
123+
:widths: 30 70
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::
158+
:widths: 30 70
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::
188+
:widths: 30 70
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>`.

Doc/library/asyncio-eventloop.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1466,7 +1466,7 @@ during 5 seconds, and then stops the event loop::
14661466

14671467
.. seealso::
14681468

1469-
A similar :ref:`current date <asyncio-date-coroutine>` example
1469+
A similar :ref:`current date <asyncio_example_sleep>` example
14701470
created with a coroutine and the :func:`run` function.
14711471

14721472

Doc/library/asyncio-exceptions.rst

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
.. currentmodule:: asyncio
22

33

4+
.. _asyncio-exceptions:
5+
46
==========
57
Exceptions
68
==========
@@ -10,7 +12,7 @@ Exceptions
1012

1113
The operation has exceeded the given deadline.
1214

13-
.. note::
15+
.. important::
1416
This exception is different from the builtin :exc:`TimeoutError`
1517
exception.
1618

@@ -23,7 +25,7 @@ Exceptions
2325
when asyncio Tasks are cancelled. In almost all situations the
2426
exception must always be re-raised.
2527

26-
.. note::
28+
.. important::
2729

2830
This exception is a subclass of :exc:`Exception`, so it can be
2931
accidentally suppressed by ``try..except`` block::

Doc/library/asyncio-queue.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,8 @@ Exceptions
140140
Examples
141141
========
142142

143+
.. _asyncio_example_queue_dist:
144+
143145
Queues can be used to distribute workload between several
144146
concurrent tasks::
145147

Doc/library/asyncio-stream.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ Streams are high-level async/await-ready primitives to work with
1010
network connections. Streams allow sending and receiving data without
1111
using callbacks or low-level protocols and transports.
1212

13+
.. _asyncio_example_stream:
14+
1315
Here is an example of a TCP echo client written using asyncio
1416
streams::
1517

Doc/library/asyncio-subprocess.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ Subprocesses
99
This section describes high-level async/await asyncio APIs to
1010
create and manage subprocesses.
1111

12+
.. _asyncio_example_subprocess_shell:
13+
1214
Here's an example of how asyncio can run a shell command and
1315
communicate its result back::
1416

Doc/library/asyncio-sync.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ Event
9494
:meth:`clear` method. The :meth:`wait` method blocks until the
9595
flag is set to *true*. The flag is set to *false* initially.
9696

97+
.. _asyncio_example_sync_event:
98+
9799
Example::
98100

99101
async def waiter(event):

Doc/library/asyncio-task.rst

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ Sleeping
165165
If *result* is provided, it is returned to the caller
166166
when the coroutine completes.
167167

168-
.. _asyncio-date-coroutine:
168+
.. _asyncio_example_sleep:
169169

170170
Example of coroutine displaying the current date every second
171171
during 5 seconds::
@@ -219,6 +219,8 @@ Running Tasks Concurrently
219219
If the *gather* itself is cancelled, the cancellation is
220220
propagated regardless of *return_exceptions*.
221221

222+
.. _asyncio_example_gather:
223+
222224
Example::
223225

224226
import asyncio
@@ -316,6 +318,8 @@ Timeouts
316318

317319
If the wait is cancelled, the future *fut* is also cancelled.
318320

321+
.. _asyncio_example_waitfor:
322+
319323
Example::
320324

321325
async def eternity():
@@ -539,6 +543,8 @@ Task Object
539543
suppressing cancellation completely is not common and is actively
540544
discouraged.
541545

546+
.. _asyncio_example_task_cancel:
547+
542548
The following example illustrates how coroutines can intercept
543549
the cancellation request::
544550

Doc/library/asyncio.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ as well as **low-level** APIs for *library and framework developers* to:
4242
with async/await syntax.
4343

4444

45-
Contents
46-
--------
45+
Reference
46+
---------
4747

4848
.. rubric:: High-level APIs
4949

@@ -73,6 +73,7 @@ Contents
7373
.. toctree::
7474
:maxdepth: 1
7575

76+
asyncio-api-index.rst
7677
asyncio-dev.rst
7778

7879

0 commit comments

Comments
 (0)