2018-09-14 17:32:07 -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:25:28 -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!')
|
|
|
|
|
2018-09-18 18:55:44 -03:00
|
|
|
# Python 3.7+
|
2018-09-17 19:41:59 -03:00
|
|
|
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
|
2018-09-14 20:57:11 -03:00
|
|
|
provide asynchronous APIs for :meth:`networking <loop.create_server>`,
|
|
|
|
running :meth:`subprocesses <loop.subprocess_exec>`,
|
|
|
|
handling :meth:`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.
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
.. 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:25:28 -03:00
|
|
|
|
|
|
|
.. note::
|
|
|
|
The source code for asyncio can be found in :source:`Lib/asyncio/`.
|