asyncio --- 异步 I/O


asyncio 是用来编写 并发 代码的库,使用 async/await 语法。

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

asyncio 往往是构建 IO 密集型和高层级 结构化 网络代码的最佳选择。

asyncio 提供一组 高层级 API 用于:

此外,还有一些 低层级 API 以支持 库和框架的开发者 实现:

你可以在 REPL 中尝试使用 asyncio 并发上下文:

$ 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'

Availability: not WASI.

This module does not work or is not available on WebAssembly. See WebAssembly 平台 for more information.

参考

备注

asyncio 的源代码可以在 Lib/asyncio/ 中找到。