2024-05-08 16:34:40 -03:00
|
|
|
:mod:`!asyncio` --- Asynchronous I/O
|
|
|
|
====================================
|
2013-11-22 15:47:22 -04:00
|
|
|
|
|
|
|
.. module:: asyncio
|
2018-09-14 17:32:07 -03:00
|
|
|
:synopsis: Asynchronous I/O.
|
2016-06-11 16:02:54 -03:00
|
|
|
|
2019-10-10 20:18:46 -03:00
|
|
|
-------------------------------
|
2013-11-22 15:47:22 -04:00
|
|
|
|
2018-09-17 19:41:59 -03:00
|
|
|
.. sidebar:: Hello World!
|
|
|
|
|
2018-09-18 03:47:54 -03:00
|
|
|
::
|
2018-09-17 19:41:59 -03:00
|
|
|
|
|
|
|
import asyncio
|
|
|
|
|
|
|
|
async def main():
|
|
|
|
print('Hello ...')
|
|
|
|
await asyncio.sleep(1)
|
|
|
|
print('... World!')
|
|
|
|
|
|
|
|
asyncio.run(main())
|
|
|
|
|
2018-09-14 18:57:39 -03:00
|
|
|
asyncio is a library to write **concurrent** code using
|
2018-09-17 20:16:44 -03:00
|
|
|
the **async/await** syntax.
|
2018-09-14 17:32:07 -03:00
|
|
|
|
2018-09-14 18:57:39 -03:00
|
|
|
asyncio is used as a foundation for multiple Python asynchronous
|
|
|
|
frameworks that provide high-performance network and web-servers,
|
|
|
|
database connection libraries, distributed task queues, etc.
|
2013-11-22 19:34:26 -04:00
|
|
|
|
2018-09-14 18:57:39 -03:00
|
|
|
asyncio is often a perfect fit for IO-bound and high-level
|
|
|
|
**structured** network code.
|
2013-11-22 19:34:26 -04:00
|
|
|
|
2018-09-14 18:57:39 -03:00
|
|
|
asyncio provides a set of **high-level** APIs to:
|
2013-11-22 19:34:26 -04:00
|
|
|
|
2018-09-14 18:57:39 -03:00
|
|
|
* :ref:`run Python coroutines <coroutine>` concurrently and
|
|
|
|
have full control over their execution;
|
2013-11-22 19:34:26 -04:00
|
|
|
|
2018-09-14 18:57:39 -03:00
|
|
|
* perform :ref:`network IO and IPC <asyncio-streams>`;
|
2013-11-22 19:34:26 -04:00
|
|
|
|
2018-09-14 18:57:39 -03:00
|
|
|
* control :ref:`subprocesses <asyncio-subprocess>`;
|
2013-11-22 19:34:26 -04:00
|
|
|
|
2018-09-14 18:57:39 -03:00
|
|
|
* distribute tasks via :ref:`queues <asyncio-queues>`;
|
2013-11-22 19:34:26 -04:00
|
|
|
|
2018-09-14 18:57:39 -03:00
|
|
|
* :ref:`synchronize <asyncio-sync>` concurrent code;
|
2013-11-22 19:34:26 -04:00
|
|
|
|
2018-09-17 20:16:44 -03:00
|
|
|
Additionally, there are **low-level** APIs for
|
|
|
|
*library and framework developers* to:
|
2013-11-22 19:45:02 -04:00
|
|
|
|
2018-09-14 18:57:39 -03:00
|
|
|
* create and manage :ref:`event loops <asyncio-event-loop>`, which
|
2023-10-24 12:22:08 -03:00
|
|
|
provide asynchronous APIs for :ref:`networking <loop_create_server>`,
|
|
|
|
running :ref:`subprocesses <loop_subprocess_exec>`,
|
|
|
|
handling :ref:`OS signals <loop_add_signal_handler>`, etc;
|
2015-02-25 09:23:51 -04:00
|
|
|
|
2018-09-14 18:57:39 -03:00
|
|
|
* implement efficient protocols using
|
|
|
|
:ref:`transports <asyncio-transports-protocols>`;
|
|
|
|
|
|
|
|
* :ref:`bridge <asyncio-futures>` callback-based libraries and code
|
|
|
|
with async/await syntax.
|
|
|
|
|
2023-09-19 08:57:28 -03:00
|
|
|
.. _asyncio-cli:
|
|
|
|
|
2023-01-23 07:31:13 -04:00
|
|
|
You can experiment with an ``asyncio`` concurrent context in the REPL:
|
|
|
|
|
|
|
|
.. code-block:: pycon
|
|
|
|
|
|
|
|
$ python -m asyncio
|
|
|
|
asyncio REPL ...
|
|
|
|
Use "await" directly instead of "asyncio.run()".
|
|
|
|
Type "help", "copyright", "credits" or "license" for more information.
|
|
|
|
>>> import asyncio
|
|
|
|
>>> await asyncio.sleep(10, result='hello')
|
|
|
|
'hello'
|
|
|
|
|
2022-08-02 16:00:41 -03:00
|
|
|
.. include:: ../includes/wasm-notavail.rst
|
2018-09-14 18:57:39 -03:00
|
|
|
|
2018-09-17 16:35:24 -03:00
|
|
|
.. We use the "rubric" directive here to avoid creating
|
|
|
|
the "Reference" subsection in the TOC.
|
2018-09-14 18:57:39 -03:00
|
|
|
|
2018-09-17 16:35:24 -03:00
|
|
|
.. rubric:: Reference
|
2013-11-22 19:34:26 -04:00
|
|
|
|
2013-12-02 20:08:00 -04:00
|
|
|
.. toctree::
|
2018-09-17 16:35:24 -03:00
|
|
|
:caption: High-level APIs
|
2018-09-14 17:32:07 -03:00
|
|
|
:maxdepth: 1
|
2013-12-02 20:08:00 -04:00
|
|
|
|
2022-03-24 16:51:16 -03:00
|
|
|
asyncio-runner.rst
|
2013-12-02 20:08:00 -04:00
|
|
|
asyncio-task.rst
|
2014-01-23 06:05:01 -04:00
|
|
|
asyncio-stream.rst
|
2013-12-02 20:08:00 -04:00
|
|
|
asyncio-sync.rst
|
2018-09-14 17:32:07 -03:00
|
|
|
asyncio-subprocess.rst
|
2015-02-25 08:55:43 -04:00
|
|
|
asyncio-queue.rst
|
2018-09-11 13:54:40 -03:00
|
|
|
asyncio-exceptions.rst
|
2013-11-22 19:34:26 -04:00
|
|
|
|
2018-09-14 17:32:07 -03:00
|
|
|
.. toctree::
|
2018-09-17 16:35:24 -03:00
|
|
|
:caption: Low-level APIs
|
2018-09-14 17:32:07 -03:00
|
|
|
:maxdepth: 1
|
2013-12-03 10:04:36 -04:00
|
|
|
|
2018-09-14 17:32:07 -03:00
|
|
|
asyncio-eventloop.rst
|
|
|
|
asyncio-future.rst
|
|
|
|
asyncio-protocol.rst
|
|
|
|
asyncio-policy.rst
|
|
|
|
asyncio-platforms.rst
|
2022-03-31 18:06:07 -03:00
|
|
|
asyncio-extending.rst
|
2018-09-14 17:32:07 -03:00
|
|
|
|
|
|
|
.. toctree::
|
2018-09-17 16:35:24 -03:00
|
|
|
:caption: Guides and Tutorials
|
2018-09-14 17:32:07 -03:00
|
|
|
:maxdepth: 1
|
|
|
|
|
2018-09-14 19:11:24 -03:00
|
|
|
asyncio-api-index.rst
|
2018-09-17 16:35:24 -03:00
|
|
|
asyncio-llapi-index.rst
|
2018-09-14 17:32:07 -03:00
|
|
|
asyncio-dev.rst
|
2019-10-10 20:18:46 -03:00
|
|
|
|
|
|
|
.. note::
|
|
|
|
The source code for asyncio can be found in :source:`Lib/asyncio/`.
|