asyncio — 异步 I/O


asyncio 库能编写 concurrent 代码使用 async/await 句法。

asyncio 被用作多种 Python 异步框架的基础,提供高性能网络和 Web 服务器、数据库连接库、分布式任务队列、等。

asyncio 通常完美适合 IO 绑定和高级 structured 网络代码。

asyncio 提供一组 高级 API:

此外,还有 低级 API 对于 库和框架开发者 to:

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'
				

可用性 :非 Emscripten,非 WASI。

本模块不工作 (或不可用) 于 WebAssembly 平台 wasm32-emscripten and wasm32-wasi 。见 WebAssembly 平台 了解更多信息。

参考

高级 API

低级 API

指南和教程

注意

可以找到 asyncio 源代码在 Lib/asyncio/ .

上一话题

网络和进程间通信

下一话题

运行器

本页