blob: bfc97001bb71edf50b7d299855abbb0fb0e38988 [file] [log] [blame]
Yury Selivanov3faaa882018-09-14 13:32:07 -07001:mod:`asyncio` --- Asynchronous I/O
2===================================
Guido van Rossum7a465642013-11-22 11:47:22 -08003
4.. module:: asyncio
Yury Selivanov3faaa882018-09-14 13:32:07 -07005 :synopsis: Asynchronous I/O.
Terry Jan Reedyfa089b92016-06-11 15:02:54 -04006
Guido van Rossumf8d0ff92013-11-22 16:53:25 -08007--------------
Guido van Rossum7a465642013-11-22 11:47:22 -08008
Yury Selivanov30855342018-09-17 18:41:59 -04009.. sidebar:: Hello World!
10
11 .. code-block:: python
12
13 import asyncio
14
15 async def main():
16 print('Hello ...')
17 await asyncio.sleep(1)
18 print('... World!')
19
20 asyncio.run(main())
21
Yury Selivanov6c731642018-09-14 14:57:39 -070022asyncio is a library to write **concurrent** code using
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -040023the **async/await** syntax.
Yury Selivanov3faaa882018-09-14 13:32:07 -070024
Yury Selivanov6c731642018-09-14 14:57:39 -070025asyncio is used as a foundation for multiple Python asynchronous
26frameworks that provide high-performance network and web-servers,
27database connection libraries, distributed task queues, etc.
Antoine Pitroubba86822013-11-23 00:34:26 +010028
Yury Selivanov6c731642018-09-14 14:57:39 -070029asyncio is often a perfect fit for IO-bound and high-level
30**structured** network code.
Antoine Pitroubba86822013-11-23 00:34:26 +010031
Yury Selivanov6c731642018-09-14 14:57:39 -070032asyncio provides a set of **high-level** APIs to:
Antoine Pitroubba86822013-11-23 00:34:26 +010033
Yury Selivanov6c731642018-09-14 14:57:39 -070034* :ref:`run Python coroutines <coroutine>` concurrently and
35 have full control over their execution;
Antoine Pitroubba86822013-11-23 00:34:26 +010036
Yury Selivanov6c731642018-09-14 14:57:39 -070037* perform :ref:`network IO and IPC <asyncio-streams>`;
Antoine Pitroubba86822013-11-23 00:34:26 +010038
Yury Selivanov6c731642018-09-14 14:57:39 -070039* control :ref:`subprocesses <asyncio-subprocess>`;
Antoine Pitroubba86822013-11-23 00:34:26 +010040
Yury Selivanov6c731642018-09-14 14:57:39 -070041* distribute tasks via :ref:`queues <asyncio-queues>`;
Antoine Pitroubba86822013-11-23 00:34:26 +010042
Yury Selivanov6c731642018-09-14 14:57:39 -070043* :ref:`synchronize <asyncio-sync>` concurrent code;
Antoine Pitroubba86822013-11-23 00:34:26 +010044
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -040045Additionally, there are **low-level** APIs for
46*library and framework developers* to:
Guido van Rossumf0f5d382013-11-22 15:45:02 -080047
Yury Selivanov6c731642018-09-14 14:57:39 -070048* create and manage :ref:`event loops <asyncio-event-loop>`, which
Yury Selivanov805e27e2018-09-14 16:57:11 -070049 provide asynchronous APIs for :meth:`networking <loop.create_server>`,
50 running :meth:`subprocesses <loop.subprocess_exec>`,
51 handling :meth:`OS signals <loop.add_signal_handler>`, etc;
Victor Stinner532c69a2015-02-25 14:23:51 +010052
Yury Selivanov6c731642018-09-14 14:57:39 -070053* implement efficient protocols using
54 :ref:`transports <asyncio-transports-protocols>`;
55
56* :ref:`bridge <asyncio-futures>` callback-based libraries and code
57 with async/await syntax.
58
59
Yury Selivanov394374e2018-09-17 15:35:24 -040060.. We use the "rubric" directive here to avoid creating
61 the "Reference" subsection in the TOC.
Yury Selivanov6c731642018-09-14 14:57:39 -070062
Yury Selivanov394374e2018-09-17 15:35:24 -040063.. rubric:: Reference
Antoine Pitroubba86822013-11-23 00:34:26 +010064
Victor Stinnerea3183f2013-12-03 01:08:00 +010065.. toctree::
Yury Selivanov394374e2018-09-17 15:35:24 -040066 :caption: High-level APIs
Yury Selivanov3faaa882018-09-14 13:32:07 -070067 :maxdepth: 1
68
69 asyncio-task.rst
70 asyncio-stream.rst
71 asyncio-sync.rst
72 asyncio-subprocess.rst
73 asyncio-queue.rst
74 asyncio-exceptions.rst
75
Yury Selivanov3faaa882018-09-14 13:32:07 -070076.. toctree::
Yury Selivanov394374e2018-09-17 15:35:24 -040077 :caption: Low-level APIs
Yury Selivanov3faaa882018-09-14 13:32:07 -070078 :maxdepth: 1
Antoine Pitroubba86822013-11-23 00:34:26 +010079
Victor Stinnerea3183f2013-12-03 01:08:00 +010080 asyncio-eventloop.rst
Yury Selivanov3faaa882018-09-14 13:32:07 -070081 asyncio-future.rst
82 asyncio-protocol.rst
Yury Selivanov7c7605f2018-09-11 09:54:40 -070083 asyncio-policy.rst
84 asyncio-platforms.rst
Yury Selivanov3faaa882018-09-14 13:32:07 -070085
Yury Selivanov3faaa882018-09-14 13:32:07 -070086.. toctree::
Yury Selivanov394374e2018-09-17 15:35:24 -040087 :caption: Guides and Tutorials
Yury Selivanov3faaa882018-09-14 13:32:07 -070088 :maxdepth: 1
89
Yury Selivanov7372c3b2018-09-14 15:11:24 -070090 asyncio-api-index.rst
Yury Selivanov394374e2018-09-17 15:35:24 -040091 asyncio-llapi-index.rst
Victor Stinnerdb39a0d2014-01-16 18:58:01 +010092 asyncio-dev.rst