blob: 6990adb21e3603db8cf3203a2a8322f1cab7c68a [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
Yury Selivanovb042cf12018-09-18 02:47:54 -040011 ::
Yury Selivanov30855342018-09-17 18:41:59 -040012
13 import asyncio
14
15 async def main():
16 print('Hello ...')
17 await asyncio.sleep(1)
18 print('... World!')
19
Yury Selivanov47150392018-09-18 17:55:44 -040020 # Python 3.7+
Yury Selivanov30855342018-09-17 18:41:59 -040021 asyncio.run(main())
22
Yury Selivanov6c731642018-09-14 14:57:39 -070023asyncio is a library to write **concurrent** code using
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -040024the **async/await** syntax.
Yury Selivanov3faaa882018-09-14 13:32:07 -070025
Yury Selivanov6c731642018-09-14 14:57:39 -070026asyncio is used as a foundation for multiple Python asynchronous
27frameworks that provide high-performance network and web-servers,
28database connection libraries, distributed task queues, etc.
Antoine Pitroubba86822013-11-23 00:34:26 +010029
Yury Selivanov6c731642018-09-14 14:57:39 -070030asyncio is often a perfect fit for IO-bound and high-level
31**structured** network code.
Antoine Pitroubba86822013-11-23 00:34:26 +010032
Yury Selivanov6c731642018-09-14 14:57:39 -070033asyncio provides a set of **high-level** APIs to:
Antoine Pitroubba86822013-11-23 00:34:26 +010034
Yury Selivanov6c731642018-09-14 14:57:39 -070035* :ref:`run Python coroutines <coroutine>` concurrently and
36 have full control over their execution;
Antoine Pitroubba86822013-11-23 00:34:26 +010037
Yury Selivanov6c731642018-09-14 14:57:39 -070038* perform :ref:`network IO and IPC <asyncio-streams>`;
Antoine Pitroubba86822013-11-23 00:34:26 +010039
Yury Selivanov6c731642018-09-14 14:57:39 -070040* control :ref:`subprocesses <asyncio-subprocess>`;
Antoine Pitroubba86822013-11-23 00:34:26 +010041
Yury Selivanov6c731642018-09-14 14:57:39 -070042* distribute tasks via :ref:`queues <asyncio-queues>`;
Antoine Pitroubba86822013-11-23 00:34:26 +010043
Yury Selivanov6c731642018-09-14 14:57:39 -070044* :ref:`synchronize <asyncio-sync>` concurrent code;
Antoine Pitroubba86822013-11-23 00:34:26 +010045
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -040046Additionally, there are **low-level** APIs for
47*library and framework developers* to:
Guido van Rossumf0f5d382013-11-22 15:45:02 -080048
Yury Selivanov6c731642018-09-14 14:57:39 -070049* create and manage :ref:`event loops <asyncio-event-loop>`, which
Yury Selivanov805e27e2018-09-14 16:57:11 -070050 provide asynchronous APIs for :meth:`networking <loop.create_server>`,
51 running :meth:`subprocesses <loop.subprocess_exec>`,
52 handling :meth:`OS signals <loop.add_signal_handler>`, etc;
Victor Stinner532c69a2015-02-25 14:23:51 +010053
Yury Selivanov6c731642018-09-14 14:57:39 -070054* implement efficient protocols using
55 :ref:`transports <asyncio-transports-protocols>`;
56
57* :ref:`bridge <asyncio-futures>` callback-based libraries and code
58 with async/await syntax.
59
60
Yury Selivanov394374e2018-09-17 15:35:24 -040061.. We use the "rubric" directive here to avoid creating
62 the "Reference" subsection in the TOC.
Yury Selivanov6c731642018-09-14 14:57:39 -070063
Yury Selivanov394374e2018-09-17 15:35:24 -040064.. rubric:: Reference
Antoine Pitroubba86822013-11-23 00:34:26 +010065
Victor Stinnerea3183f2013-12-03 01:08:00 +010066.. toctree::
Yury Selivanov394374e2018-09-17 15:35:24 -040067 :caption: High-level APIs
Yury Selivanov3faaa882018-09-14 13:32:07 -070068 :maxdepth: 1
69
70 asyncio-task.rst
71 asyncio-stream.rst
72 asyncio-sync.rst
73 asyncio-subprocess.rst
74 asyncio-queue.rst
75 asyncio-exceptions.rst
76
Yury Selivanov3faaa882018-09-14 13:32:07 -070077.. toctree::
Yury Selivanov394374e2018-09-17 15:35:24 -040078 :caption: Low-level APIs
Yury Selivanov3faaa882018-09-14 13:32:07 -070079 :maxdepth: 1
Antoine Pitroubba86822013-11-23 00:34:26 +010080
Victor Stinnerea3183f2013-12-03 01:08:00 +010081 asyncio-eventloop.rst
Yury Selivanov3faaa882018-09-14 13:32:07 -070082 asyncio-future.rst
83 asyncio-protocol.rst
Yury Selivanov7c7605f2018-09-11 09:54:40 -070084 asyncio-policy.rst
85 asyncio-platforms.rst
Yury Selivanov3faaa882018-09-14 13:32:07 -070086
Yury Selivanov3faaa882018-09-14 13:32:07 -070087.. toctree::
Yury Selivanov394374e2018-09-17 15:35:24 -040088 :caption: Guides and Tutorials
Yury Selivanov3faaa882018-09-14 13:32:07 -070089 :maxdepth: 1
90
Yury Selivanov7372c3b2018-09-14 15:11:24 -070091 asyncio-api-index.rst
Yury Selivanov394374e2018-09-17 15:35:24 -040092 asyncio-llapi-index.rst
Victor Stinnerdb39a0d2014-01-16 18:58:01 +010093 asyncio-dev.rst