bpo-33649: Add high-level APIs cheat-sheet (GH-9319)
This commit is contained in:
parent
6c7316439d
commit
7372c3bbef
206
Doc/library/asyncio-api-index.rst
Normal file
206
Doc/library/asyncio-api-index.rst
Normal file
@ -0,0 +1,206 @@
|
|||||||
|
.. currentmodule:: asyncio
|
||||||
|
|
||||||
|
|
||||||
|
=====================
|
||||||
|
High-level APIs Index
|
||||||
|
=====================
|
||||||
|
|
||||||
|
This page lists all high-level async/await enabled asyncio APIs.
|
||||||
|
|
||||||
|
|
||||||
|
Tasks
|
||||||
|
=====
|
||||||
|
|
||||||
|
Utilities to run asyncio programs, create Tasks, and
|
||||||
|
await on multiple things with timeouts.
|
||||||
|
|
||||||
|
.. list-table::
|
||||||
|
:widths: 30 70
|
||||||
|
|
||||||
|
* - :func:`run`
|
||||||
|
- Create event loop, run a coroutine, close the loop.
|
||||||
|
|
||||||
|
* - :func:`create_task`
|
||||||
|
- Start an asyncio Task.
|
||||||
|
|
||||||
|
* - ``await`` :func:`sleep`
|
||||||
|
- Sleep for a number of seconds.
|
||||||
|
|
||||||
|
* - ``await`` :func:`gather`
|
||||||
|
- Schedule and wait for things concurrently.
|
||||||
|
|
||||||
|
* - ``await`` :func:`wait_for`
|
||||||
|
- Run with a timeout.
|
||||||
|
|
||||||
|
* - ``await`` :func:`shield`
|
||||||
|
- Shield from cancellation.
|
||||||
|
|
||||||
|
* - ``await`` :func:`wait`
|
||||||
|
- Monitor for completeness.
|
||||||
|
|
||||||
|
* - :func:`current_task`
|
||||||
|
- Return the current Task.
|
||||||
|
|
||||||
|
* - :func:`all_tasks`
|
||||||
|
- Return all tasks for an event loop.
|
||||||
|
|
||||||
|
* - :class:`Task`
|
||||||
|
- Task object.
|
||||||
|
|
||||||
|
|
||||||
|
.. rubric:: Examples
|
||||||
|
|
||||||
|
* :ref:`Using asyncio.gather() to run things in parallel
|
||||||
|
<asyncio_example_gather>`.
|
||||||
|
|
||||||
|
* :ref:`Using asyncio.wait_for() to enforce a timeout
|
||||||
|
<asyncio_example_waitfor>`.
|
||||||
|
|
||||||
|
* :ref:`Cancellation <asyncio_example_task_cancel>`.
|
||||||
|
|
||||||
|
* :ref:`Using asyncio.sleep() <asyncio_example_sleep>`.
|
||||||
|
|
||||||
|
* See also the main :ref:`Tasks documentation page <coroutine>`.
|
||||||
|
|
||||||
|
|
||||||
|
Queues
|
||||||
|
======
|
||||||
|
|
||||||
|
Queues should be used to distribute work amongst multiple asyncio Tasks,
|
||||||
|
implement connection pools, and pub/sub patterns.
|
||||||
|
|
||||||
|
|
||||||
|
.. list-table::
|
||||||
|
:widths: 30 70
|
||||||
|
|
||||||
|
* - :class:`Queue`
|
||||||
|
- A FIFO queue.
|
||||||
|
|
||||||
|
* - :class:`PriorityQueue`
|
||||||
|
- A priority queue.
|
||||||
|
|
||||||
|
* - :class:`LifoQueue`
|
||||||
|
- A LIFO queue.
|
||||||
|
|
||||||
|
|
||||||
|
.. rubric:: Examples
|
||||||
|
|
||||||
|
* :ref:`Using asyncio.Queue to distribute workload between several
|
||||||
|
Tasks <asyncio_example_queue_dist>`.
|
||||||
|
|
||||||
|
* See also the :ref:`Queues documentation page <asyncio-queues>`.
|
||||||
|
|
||||||
|
|
||||||
|
Subprocesses
|
||||||
|
============
|
||||||
|
|
||||||
|
Utilities to spawn subprocesses and run shell commands.
|
||||||
|
|
||||||
|
.. list-table::
|
||||||
|
:widths: 30 70
|
||||||
|
|
||||||
|
* - ``await`` :func:`create_subprocess_exec`
|
||||||
|
- Create a subprocess.
|
||||||
|
|
||||||
|
* - ``await`` :func:`create_subprocess_shell`
|
||||||
|
- Run a shell command.
|
||||||
|
|
||||||
|
|
||||||
|
.. rubric:: Examples
|
||||||
|
|
||||||
|
* :ref:`Executing a shell command <asyncio_example_subprocess_shell>`.
|
||||||
|
|
||||||
|
* See also the :ref:`subprocess APIs <asyncio-subprocess>`
|
||||||
|
documentation.
|
||||||
|
|
||||||
|
|
||||||
|
Streams
|
||||||
|
=======
|
||||||
|
|
||||||
|
High-level APIs to work with network IO.
|
||||||
|
|
||||||
|
.. list-table::
|
||||||
|
:widths: 30 70
|
||||||
|
|
||||||
|
* - ``await`` :func:`open_connection`
|
||||||
|
- Establish a TCP connection.
|
||||||
|
|
||||||
|
* - ``await`` :func:`open_unix_connection`
|
||||||
|
- Establish a Unix socket connection.
|
||||||
|
|
||||||
|
* - ``await`` :func:`start_server`
|
||||||
|
- Start a TCP server.
|
||||||
|
|
||||||
|
* - ``await`` :func:`start_unix_server`
|
||||||
|
- Start a Unix socket server.
|
||||||
|
|
||||||
|
* - :class:`StreamReader`
|
||||||
|
- High-level async/await object to receive network data.
|
||||||
|
|
||||||
|
* - :class:`StreamWriter`
|
||||||
|
- High-level async/await object to send network data.
|
||||||
|
|
||||||
|
|
||||||
|
.. rubric:: Examples
|
||||||
|
|
||||||
|
* :ref:`Example TCP client <asyncio_example_stream>`.
|
||||||
|
|
||||||
|
* See also the :ref:`streams APIs <asyncio-streams>`
|
||||||
|
documentation.
|
||||||
|
|
||||||
|
|
||||||
|
Synchronization
|
||||||
|
===============
|
||||||
|
|
||||||
|
Threading-like synchronization primitives that can be used in Tasks.
|
||||||
|
|
||||||
|
.. list-table::
|
||||||
|
:widths: 30 70
|
||||||
|
|
||||||
|
* - :class:`Lock`
|
||||||
|
- A mutex lock.
|
||||||
|
|
||||||
|
* - :class:`Event`
|
||||||
|
- An event object.
|
||||||
|
|
||||||
|
* - :class:`Condition`
|
||||||
|
- A condition object.
|
||||||
|
|
||||||
|
* - :class:`Semaphore`
|
||||||
|
- A semaphore.
|
||||||
|
|
||||||
|
* - :class:`BoundedSemaphore`
|
||||||
|
- A bounded semaphore.
|
||||||
|
|
||||||
|
|
||||||
|
.. rubric:: Examples
|
||||||
|
|
||||||
|
* :ref:`Using asyncio.Event <asyncio_example_sync_event>`.
|
||||||
|
|
||||||
|
* See also the documentation of asyncio
|
||||||
|
:ref:`synchronization primitives <asyncio-sync>`.
|
||||||
|
|
||||||
|
|
||||||
|
Exceptions
|
||||||
|
==========
|
||||||
|
|
||||||
|
.. list-table::
|
||||||
|
:widths: 30 70
|
||||||
|
|
||||||
|
|
||||||
|
* - :exc:`asyncio.TimeoutError`
|
||||||
|
- Raised on timeout by functions like :func:`wait_for`.
|
||||||
|
Keep in mind that ``asyncio.TimeoutError`` is **unrelated**
|
||||||
|
to the built-in :exc:`TimeoutError` exception.
|
||||||
|
|
||||||
|
* - :exc:`asyncio.CancelledError`
|
||||||
|
- Raised when a Task is cancelled. See also :meth:`Task.cancel`.
|
||||||
|
|
||||||
|
|
||||||
|
.. rubric:: Examples
|
||||||
|
|
||||||
|
* :ref:`Handling CancelledError to run code on cancellation request
|
||||||
|
<asyncio_example_task_cancel>`.
|
||||||
|
|
||||||
|
* See also the full list of
|
||||||
|
:ref:`asyncio-specific exceptions <asyncio-exceptions>`.
|
@ -1466,7 +1466,7 @@ during 5 seconds, and then stops the event loop::
|
|||||||
|
|
||||||
.. seealso::
|
.. seealso::
|
||||||
|
|
||||||
A similar :ref:`current date <asyncio-date-coroutine>` example
|
A similar :ref:`current date <asyncio_example_sleep>` example
|
||||||
created with a coroutine and the :func:`run` function.
|
created with a coroutine and the :func:`run` function.
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
.. currentmodule:: asyncio
|
.. currentmodule:: asyncio
|
||||||
|
|
||||||
|
|
||||||
|
.. _asyncio-exceptions:
|
||||||
|
|
||||||
==========
|
==========
|
||||||
Exceptions
|
Exceptions
|
||||||
==========
|
==========
|
||||||
@ -10,7 +12,7 @@ Exceptions
|
|||||||
|
|
||||||
The operation has exceeded the given deadline.
|
The operation has exceeded the given deadline.
|
||||||
|
|
||||||
.. note::
|
.. important::
|
||||||
This exception is different from the builtin :exc:`TimeoutError`
|
This exception is different from the builtin :exc:`TimeoutError`
|
||||||
exception.
|
exception.
|
||||||
|
|
||||||
@ -23,7 +25,7 @@ Exceptions
|
|||||||
when asyncio Tasks are cancelled. In almost all situations the
|
when asyncio Tasks are cancelled. In almost all situations the
|
||||||
exception must always be re-raised.
|
exception must always be re-raised.
|
||||||
|
|
||||||
.. note::
|
.. important::
|
||||||
|
|
||||||
This exception is a subclass of :exc:`Exception`, so it can be
|
This exception is a subclass of :exc:`Exception`, so it can be
|
||||||
accidentally suppressed by ``try..except`` block::
|
accidentally suppressed by ``try..except`` block::
|
||||||
|
@ -140,6 +140,8 @@ Exceptions
|
|||||||
Examples
|
Examples
|
||||||
========
|
========
|
||||||
|
|
||||||
|
.. _asyncio_example_queue_dist:
|
||||||
|
|
||||||
Queues can be used to distribute workload between several
|
Queues can be used to distribute workload between several
|
||||||
concurrent tasks::
|
concurrent tasks::
|
||||||
|
|
||||||
|
@ -10,6 +10,8 @@ Streams are high-level async/await-ready primitives to work with
|
|||||||
network connections. Streams allow sending and receiving data without
|
network connections. Streams allow sending and receiving data without
|
||||||
using callbacks or low-level protocols and transports.
|
using callbacks or low-level protocols and transports.
|
||||||
|
|
||||||
|
.. _asyncio_example_stream:
|
||||||
|
|
||||||
Here is an example of a TCP echo client written using asyncio
|
Here is an example of a TCP echo client written using asyncio
|
||||||
streams::
|
streams::
|
||||||
|
|
||||||
|
@ -9,6 +9,8 @@ Subprocesses
|
|||||||
This section describes high-level async/await asyncio APIs to
|
This section describes high-level async/await asyncio APIs to
|
||||||
create and manage subprocesses.
|
create and manage subprocesses.
|
||||||
|
|
||||||
|
.. _asyncio_example_subprocess_shell:
|
||||||
|
|
||||||
Here's an example of how asyncio can run a shell command and
|
Here's an example of how asyncio can run a shell command and
|
||||||
communicate its result back::
|
communicate its result back::
|
||||||
|
|
||||||
|
@ -94,6 +94,8 @@ Event
|
|||||||
:meth:`clear` method. The :meth:`wait` method blocks until the
|
:meth:`clear` method. The :meth:`wait` method blocks until the
|
||||||
flag is set to *true*. The flag is set to *false* initially.
|
flag is set to *true*. The flag is set to *false* initially.
|
||||||
|
|
||||||
|
.. _asyncio_example_sync_event:
|
||||||
|
|
||||||
Example::
|
Example::
|
||||||
|
|
||||||
async def waiter(event):
|
async def waiter(event):
|
||||||
|
@ -165,7 +165,7 @@ Sleeping
|
|||||||
If *result* is provided, it is returned to the caller
|
If *result* is provided, it is returned to the caller
|
||||||
when the coroutine completes.
|
when the coroutine completes.
|
||||||
|
|
||||||
.. _asyncio-date-coroutine:
|
.. _asyncio_example_sleep:
|
||||||
|
|
||||||
Example of coroutine displaying the current date every second
|
Example of coroutine displaying the current date every second
|
||||||
during 5 seconds::
|
during 5 seconds::
|
||||||
@ -219,6 +219,8 @@ Running Tasks Concurrently
|
|||||||
If the *gather* itself is cancelled, the cancellation is
|
If the *gather* itself is cancelled, the cancellation is
|
||||||
propagated regardless of *return_exceptions*.
|
propagated regardless of *return_exceptions*.
|
||||||
|
|
||||||
|
.. _asyncio_example_gather:
|
||||||
|
|
||||||
Example::
|
Example::
|
||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
@ -316,6 +318,8 @@ Timeouts
|
|||||||
|
|
||||||
If the wait is cancelled, the future *fut* is also cancelled.
|
If the wait is cancelled, the future *fut* is also cancelled.
|
||||||
|
|
||||||
|
.. _asyncio_example_waitfor:
|
||||||
|
|
||||||
Example::
|
Example::
|
||||||
|
|
||||||
async def eternity():
|
async def eternity():
|
||||||
@ -539,6 +543,8 @@ Task Object
|
|||||||
suppressing cancellation completely is not common and is actively
|
suppressing cancellation completely is not common and is actively
|
||||||
discouraged.
|
discouraged.
|
||||||
|
|
||||||
|
.. _asyncio_example_task_cancel:
|
||||||
|
|
||||||
The following example illustrates how coroutines can intercept
|
The following example illustrates how coroutines can intercept
|
||||||
the cancellation request::
|
the cancellation request::
|
||||||
|
|
||||||
|
@ -42,8 +42,8 @@ as well as **low-level** APIs for *library and framework developers* to:
|
|||||||
with async/await syntax.
|
with async/await syntax.
|
||||||
|
|
||||||
|
|
||||||
Contents
|
Reference
|
||||||
--------
|
---------
|
||||||
|
|
||||||
.. rubric:: High-level APIs
|
.. rubric:: High-level APIs
|
||||||
|
|
||||||
@ -73,6 +73,7 @@ Contents
|
|||||||
.. toctree::
|
.. toctree::
|
||||||
:maxdepth: 1
|
:maxdepth: 1
|
||||||
|
|
||||||
|
asyncio-api-index.rst
|
||||||
asyncio-dev.rst
|
asyncio-dev.rst
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user