asyncio
— 异步 I/O
¶
asyncio 库能编写 concurrent 代码使用 async/await 句法。
asyncio 被用作多种 Python 异步框架的基础,提供高性能网络和 Web 服务器、数据库连接库、分布式任务队列、等。
asyncio 通常完美适合 IO 绑定和高级 structured 网络代码。
asyncio 提供一组 高级 API:
运行 Python 协程 并发且完整控制它们的执行;
履行 网络 IO 和 IPC (进程间通信) ;
控制 子进程 ;
分发任务凭借 队列 ;
同步 并发代码;
此外,还有 低级 API 对于 库和框架开发者 to:
创建和管理 事件循环 ,提供异步 API 为 networking ,运行 子进程 ,处理 OS signals ,等;
实现高效协议使用 传输 ;
bridge 基于回调的库和采用 async/await 句法的代码。
可用性 :非 WASI。
This module does not work or is not available on WebAssembly. See WebAssembly 平台 了解更多信息。
asyncio REPL
You can experiment with an
asyncio
concurrent context in the
REPL
:
$ python -m asyncio
asyncio REPL ...
Use "await" directly instead of "asyncio.run()".
Type "help", "copyright", "credits" or "license" for more information.
>>> import asyncio
>>> await asyncio.sleep(10, result='hello')
'hello'
引发
审计事件
cpython.run_stdin
不带自变量。
Changed in version 3.12.5: (also 3.11.10, 3.10.15, 3.9.20, and 3.8.20) Emits audit events.
Changed in version 3.13:
Uses PyREPL if possible, in which case
PYTHONSTARTUP
is also executed. Emits audit events.
参考
指南和教程
注意
可以找到 asyncio 源代码在 Lib/asyncio/ .