Yury Selivanov | 512d710 | 2018-09-17 19:35:30 -0400 | [diff] [blame^] | 1 | :mod:`asyncio` --- Asynchronous I/O |
| 2 | =================================== |
Guido van Rossum | 7a46564 | 2013-11-22 11:47:22 -0800 | [diff] [blame] | 3 | |
| 4 | .. module:: asyncio |
Yury Selivanov | 512d710 | 2018-09-17 19:35:30 -0400 | [diff] [blame^] | 5 | :synopsis: Asynchronous I/O. |
Terry Jan Reedy | fa089b9 | 2016-06-11 15:02:54 -0400 | [diff] [blame] | 6 | |
Guido van Rossum | f8d0ff9 | 2013-11-22 16:53:25 -0800 | [diff] [blame] | 7 | -------------- |
Guido van Rossum | 7a46564 | 2013-11-22 11:47:22 -0800 | [diff] [blame] | 8 | |
Yury Selivanov | 512d710 | 2018-09-17 19:35:30 -0400 | [diff] [blame^] | 9 | .. sidebar:: Hello World! |
Antoine Pitrou | bba8682 | 2013-11-23 00:34:26 +0100 | [diff] [blame] | 10 | |
Yury Selivanov | 512d710 | 2018-09-17 19:35:30 -0400 | [diff] [blame^] | 11 | .. code-block:: python |
Antoine Pitrou | bba8682 | 2013-11-23 00:34:26 +0100 | [diff] [blame] | 12 | |
Yury Selivanov | 512d710 | 2018-09-17 19:35:30 -0400 | [diff] [blame^] | 13 | import asyncio |
Antoine Pitrou | bba8682 | 2013-11-23 00:34:26 +0100 | [diff] [blame] | 14 | |
Yury Selivanov | 512d710 | 2018-09-17 19:35:30 -0400 | [diff] [blame^] | 15 | async def main(): |
| 16 | print('Hello ...') |
| 17 | await asyncio.sleep(1) |
| 18 | print('... World!') |
Antoine Pitrou | bba8682 | 2013-11-23 00:34:26 +0100 | [diff] [blame] | 19 | |
Yury Selivanov | 512d710 | 2018-09-17 19:35:30 -0400 | [diff] [blame^] | 20 | asyncio.run(main()) |
Antoine Pitrou | bba8682 | 2013-11-23 00:34:26 +0100 | [diff] [blame] | 21 | |
Yury Selivanov | 512d710 | 2018-09-17 19:35:30 -0400 | [diff] [blame^] | 22 | asyncio is a library to write **concurrent** code using |
| 23 | the **async/await** syntax. |
Antoine Pitrou | bba8682 | 2013-11-23 00:34:26 +0100 | [diff] [blame] | 24 | |
Yury Selivanov | 512d710 | 2018-09-17 19:35:30 -0400 | [diff] [blame^] | 25 | asyncio is used as a foundation for multiple Python asynchronous |
| 26 | frameworks that provide high-performance network and web-servers, |
| 27 | database connection libraries, distributed task queues, etc. |
Antoine Pitrou | bba8682 | 2013-11-23 00:34:26 +0100 | [diff] [blame] | 28 | |
Yury Selivanov | 512d710 | 2018-09-17 19:35:30 -0400 | [diff] [blame^] | 29 | asyncio is often a perfect fit for IO-bound and high-level |
| 30 | **structured** network code. |
Antoine Pitrou | bba8682 | 2013-11-23 00:34:26 +0100 | [diff] [blame] | 31 | |
Yury Selivanov | 512d710 | 2018-09-17 19:35:30 -0400 | [diff] [blame^] | 32 | asyncio provides a set of **high-level** APIs to: |
Guido van Rossum | f0f5d38 | 2013-11-22 15:45:02 -0800 | [diff] [blame] | 33 | |
Yury Selivanov | 512d710 | 2018-09-17 19:35:30 -0400 | [diff] [blame^] | 34 | * :ref:`run Python coroutines <coroutine>` concurrently and |
| 35 | have full control over their execution; |
Victor Stinner | 532c69a | 2015-02-25 14:23:51 +0100 | [diff] [blame] | 36 | |
Yury Selivanov | 512d710 | 2018-09-17 19:35:30 -0400 | [diff] [blame^] | 37 | * perform :ref:`network IO and IPC <asyncio-streams>`; |
| 38 | |
| 39 | * control :ref:`subprocesses <asyncio-subprocess>`; |
| 40 | |
| 41 | * distribute tasks via :ref:`queues <asyncio-queues>`; |
| 42 | |
| 43 | * :ref:`synchronize <asyncio-sync>` concurrent code; |
| 44 | |
| 45 | Additionally, there are **low-level** APIs for |
| 46 | *library and framework developers* to: |
| 47 | |
| 48 | * create and manage :ref:`event loops <asyncio-event-loop>`, which |
| 49 | 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; |
| 52 | |
| 53 | * 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 | |
| 60 | .. We use the "rubric" directive here to avoid creating |
| 61 | the "Reference" subsection in the TOC. |
| 62 | |
| 63 | .. rubric:: Reference |
Antoine Pitrou | bba8682 | 2013-11-23 00:34:26 +0100 | [diff] [blame] | 64 | |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 65 | .. toctree:: |
Yury Selivanov | 512d710 | 2018-09-17 19:35:30 -0400 | [diff] [blame^] | 66 | :caption: High-level APIs |
| 67 | :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 | |
| 76 | .. toctree:: |
| 77 | :caption: Low-level APIs |
| 78 | :maxdepth: 1 |
Antoine Pitrou | bba8682 | 2013-11-23 00:34:26 +0100 | [diff] [blame] | 79 | |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 80 | asyncio-eventloop.rst |
Yury Selivanov | 512d710 | 2018-09-17 19:35:30 -0400 | [diff] [blame^] | 81 | asyncio-future.rst |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 82 | asyncio-protocol.rst |
Yury Selivanov | 512d710 | 2018-09-17 19:35:30 -0400 | [diff] [blame^] | 83 | asyncio-policy.rst |
| 84 | asyncio-platforms.rst |
| 85 | |
| 86 | .. toctree:: |
| 87 | :caption: Guides and Tutorials |
| 88 | :maxdepth: 1 |
| 89 | |
| 90 | asyncio-api-index.rst |
| 91 | asyncio-llapi-index.rst |
Victor Stinner | db39a0d | 2014-01-16 18:58:01 +0100 | [diff] [blame] | 92 | asyncio-dev.rst |