新聞中心
asyncio —- 異步 I/O
Hello World!

import asyncioasync def main():print('Hello ...')await asyncio.sleep(1)print('... World!')asyncio.run(main())
asyncio 是用來編寫 并發(fā) 代碼的庫,使用 async/await 語法。
asyncio 被用作多個提供高性能 python 異步框架的基礎,包括網(wǎng)絡和網(wǎng)站服務,數(shù)據(jù)庫連接庫,分布式任務隊列等等。
asyncio 往往是構(gòu)建 IO 密集型和高層級 結(jié)構(gòu)化 網(wǎng)絡代碼的最佳選擇。
asyncio 提供一組 高層級 API 用于:
-
并發(fā)地 運行 Python 協(xié)程 并對其執(zhí)行過程實現(xiàn)完全控制;
-
執(zhí)行 網(wǎng)絡 IO 和 IPC;
-
控制 子進程;
-
通過 隊列 實現(xiàn)分布式任務;
-
同步 并發(fā)代碼;
此外,還有一些 低層級 API 以支持 庫和框架的開發(fā)者 實現(xiàn):
-
創(chuàng)建和管理 事件循環(huán),以提供異步 API 用于 網(wǎng)絡化, 運行 子進程,處理 OS 信號 等等;
-
使用 transports 實現(xiàn)高效率協(xié)議;
-
通過 async/await 語法 橋接 基于回調(diào)的庫和代碼。
Availability: not Emscripten, not WASI.
This module does not work or is not available on WebAssembly platforms wasm32-emscripten and wasm32-wasi. See WebAssembly platforms for more information.
參考
高層級 API
- Runners
- 協(xié)程與任務
- 流
- 同步原語
- 子進程集
- 隊列集
- 異常
低層級 API
- 事件循環(huán)
- Futures
- 傳輸和協(xié)議
- 策略
- 平臺支持
- Extending
指南與教程
- 高層級 API 索引
- 低層級 API 索引
- 用 asyncio 開發(fā)
備注
asyncio 的源代碼可以在 Lib/asyncio/ 中找到。
文章題目:創(chuàng)新互聯(lián)Python教程:asyncio—-異步I/O
鏈接地址:http://www.dlmjj.cn/article/cdgjhgo.html


咨詢
建站咨詢
