blob: 6990adb21e3603db8cf3203a2a8322f1cab7c68a [file] [log] [blame]
Yury Selivanov512d7102018-09-17 19:35:30 -04001:mod:`asyncio` --- Asynchronous I/O
2===================================
Guido van Rossum7a465642013-11-22 11:47:22 -08003
4.. module:: asyncio
Yury Selivanov512d7102018-09-17 19:35:30 -04005 :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 Selivanov512d7102018-09-17 19:35:30 -04009.. sidebar:: Hello World!
Antoine Pitroubba86822013-11-23 00:34:26 +010010
Miss Islington (bot)45452b72018-09-18 00:00:58 -070011 ::
Antoine Pitroubba86822013-11-23 00:34:26 +010012
Yury Selivanov512d7102018-09-17 19:35:30 -040013 import asyncio
Antoine Pitroubba86822013-11-23 00:34:26 +010014
Yury Selivanov512d7102018-09-17 19:35:30 -040015 async def main():
16 print('Hello ...')
17 await asyncio.sleep(1)
18 print('... World!')
Antoine Pitroubba86822013-11-23 00:34:26 +010019
Miss Islington (bot)73c00062018-09-18 15:09:51 -070020 # Python 3.7+
Yury Selivanov512d7102018-09-17 19:35:30 -040021 asyncio.run(main())
Antoine Pitroubba86822013-11-23 00:34:26 +010022
Yury Selivanov512d7102018-09-17 19:35:30 -040023asyncio is a library to write **concurrent** code using
24the **async/await** syntax.
Antoine Pitroubba86822013-11-23 00:34:26 +010025
Yury Selivanov512d7102018-09-17 19:35:30 -040026asyncio 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 Selivanov512d7102018-09-17 19:35:30 -040030asyncio is often a perfect fit for IO-bound and high-level
31**structured** network code.
Antoine Pitroubba86822013-11-23 00:34:26 +010032
Yury Selivanov512d7102018-09-17 19:35:30 -040033asyncio provides a set of **high-level** APIs to:
Guido van Rossumf0f5d382013-11-22 15:45:02 -080034
Yury Selivanov512d7102018-09-17 19:35:30 -040035* :ref:`run Python coroutines <coroutine>` concurrently and
36 have full control over their execution;
Victor Stinner532c69a2015-02-25 14:23:51 +010037
Yury Selivanov512d7102018-09-17 19:35:30 -040038* perform :ref:`network IO and IPC <asyncio-streams>`;
39
40* control :ref:`subprocesses <asyncio-subprocess>`;
41
42* distribute tasks via :ref:`queues <asyncio-queues>`;
43
44* :ref:`synchronize <asyncio-sync>` concurrent code;
45
46Additionally, there are **low-level** APIs for
47*library and framework developers* to:
48
49* create and manage :ref:`event loops <asyncio-event-loop>`, which
50 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;
53
54* 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
61.. We use the "rubric" directive here to avoid creating
62 the "Reference" subsection in the TOC.
63
64.. rubric:: Reference
Antoine Pitroubba86822013-11-23 00:34:26 +010065
Victor Stinnerea3183f2013-12-03 01:08:00 +010066.. toctree::
Yury Selivanov512d7102018-09-17 19:35:30 -040067 :caption: High-level APIs
68 :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
77.. toctree::
78 :caption: Low-level APIs
79 :maxdepth: 1
Antoine Pitroubba86822013-11-23 00:34:26 +010080
Victor Stinnerea3183f2013-12-03 01:08:00 +010081 asyncio-eventloop.rst
Yury Selivanov512d7102018-09-17 19:35:30 -040082 asyncio-future.rst
Victor Stinnerea3183f2013-12-03 01:08:00 +010083 asyncio-protocol.rst
Yury Selivanov512d7102018-09-17 19:35:30 -040084 asyncio-policy.rst
85 asyncio-platforms.rst
86
87.. toctree::
88 :caption: Guides and Tutorials
89 :maxdepth: 1
90
91 asyncio-api-index.rst
92 asyncio-llapi-index.rst
Victor Stinnerdb39a0d2014-01-16 18:58:01 +010093 asyncio-dev.rst