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

import asyncioasync def main():print('Hello ...')await asyncio.sleep(1)print('... World!')asyncio.run(main())
asyncio 是用來編寫 并發(fā) 代碼的庫(kù),使用 async/await 語(yǔ)法。
asyncio 被用作多個(gè)提供高性能 python 異步框架的基礎(chǔ),包括網(wǎng)絡(luò)和網(wǎng)站服務(wù),數(shù)據(jù)庫(kù)連接庫(kù),分布式任務(wù)隊(duì)列等等。
asyncio 往往是構(gòu)建 IO 密集型和高層級(jí) 結(jié)構(gòu)化 網(wǎng)絡(luò)代碼的最佳選擇。
asyncio 提供一組 高層級(jí) API 用于:
-
并發(fā)地 運(yùn)行 Python 協(xié)程 并對(duì)其執(zhí)行過程實(shí)現(xiàn)完全控制;
-
執(zhí)行 網(wǎng)絡(luò) IO 和 IPC;
-
控制 子進(jìn)程;
-
通過 隊(duì)列 實(shí)現(xiàn)分布式任務(wù);
-
同步 并發(fā)代碼;
此外,還有一些 低層級(jí) API 以支持 庫(kù)和框架的開發(fā)者 實(shí)現(xiàn):
-
創(chuàng)建和管理 事件循環(huán),以提供異步 API 用于 網(wǎng)絡(luò)化, 運(yùn)行 子進(jìn)程,處理 OS 信號(hào) 等等;
-
使用 transports 實(shí)現(xiàn)高效率協(xié)議;
-
通過 async/await 語(yǔ)法 橋接 基于回調(diào)的庫(kù)和代碼。
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.
參考
高層級(jí) API
- Runners
- 協(xié)程與任務(wù)
- 流
- 同步原語(yǔ)
- 子進(jìn)程集
- 隊(duì)列集
- 異常
低層級(jí) API
- 事件循環(huán)
- Futures
- 傳輸和協(xié)議
- 策略
- 平臺(tái)支持
- Extending
指南與教程
- 高層級(jí) API 索引
- 低層級(jí) API 索引
- 用 asyncio 開發(fā)
備注
asyncio 的源代碼可以在 Lib/asyncio/ 中找到。
網(wǎng)站標(biāo)題:創(chuàng)新互聯(lián)Python教程:asyncio—-異步I/O
轉(zhuǎn)載注明:http://m.fisionsoft.com.cn/article/cdgjhgo.html


咨詢
建站咨詢
