blob: 73b0e63a68dbe7fab5796c775f6692cff9614b83 [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
23**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
Yury Selivanov6c731642018-09-14 14:57:39 -070045as well as **low-level** APIs for *library and framework developers* to:
Guido van Rossumf0f5d382013-11-22 15:45:02 -080046
Yury Selivanov6c731642018-09-14 14:57:39 -070047* create and manage :ref:`event loops <asyncio-event-loop>`, which
Yury Selivanov805e27e2018-09-14 16:57:11 -070048 provide asynchronous APIs for :meth:`networking <loop.create_server>`,
49 running :meth:`subprocesses <loop.subprocess_exec>`,
50 handling :meth:`OS signals <loop.add_signal_handler>`, etc;
Victor Stinner532c69a2015-02-25 14:23:51 +010051
Yury Selivanov6c731642018-09-14 14:57:39 -070052* implement efficient protocols using
53 :ref:`transports <asyncio-transports-protocols>`;
54
55* :ref:`bridge <asyncio-futures>` callback-based libraries and code
56 with async/await syntax.
57
58
Yury Selivanov394374e2018-09-17 15:35:24 -040059.. We use the "rubric" directive here to avoid creating
60 the "Reference" subsection in the TOC.
Yury Selivanov6c731642018-09-14 14:57:39 -070061
Yury Selivanov394374e2018-09-17 15:35:24 -040062.. rubric:: Reference
Antoine Pitroubba86822013-11-23 00:34:26 +010063
Victor Stinnerea3183f2013-12-03 01:08:00 +010064.. toctree::
Yury Selivanov394374e2018-09-17 15:35:24 -040065 :caption: High-level APIs
Yury Selivanov3faaa882018-09-14 13:32:07 -070066 :maxdepth: 1
67
68 asyncio-task.rst
69 asyncio-stream.rst
70 asyncio-sync.rst
71 asyncio-subprocess.rst
72 asyncio-queue.rst
73 asyncio-exceptions.rst
74
Yury Selivanov3faaa882018-09-14 13:32:07 -070075.. toctree::
Yury Selivanov394374e2018-09-17 15:35:24 -040076 :caption: Low-level APIs
Yury Selivanov3faaa882018-09-14 13:32:07 -070077 :maxdepth: 1
Antoine Pitroubba86822013-11-23 00:34:26 +010078
Victor Stinnerea3183f2013-12-03 01:08:00 +010079 asyncio-eventloop.rst
Yury Selivanov3faaa882018-09-14 13:32:07 -070080 asyncio-future.rst
81 asyncio-protocol.rst
Yury Selivanov7c7605f2018-09-11 09:54:40 -070082 asyncio-policy.rst
83 asyncio-platforms.rst
Yury Selivanov3faaa882018-09-14 13:32:07 -070084
Yury Selivanov3faaa882018-09-14 13:32:07 -070085.. toctree::
Yury Selivanov394374e2018-09-17 15:35:24 -040086 :caption: Guides and Tutorials
Yury Selivanov3faaa882018-09-14 13:32:07 -070087 :maxdepth: 1
88
Yury Selivanov7372c3b2018-09-14 15:11:24 -070089 asyncio-api-index.rst
Yury Selivanov394374e2018-09-17 15:35:24 -040090 asyncio-llapi-index.rst
Victor Stinnerdb39a0d2014-01-16 18:58:01 +010091 asyncio-dev.rst