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