asyncio --- 非同步 I/O


asyncio 是讓使用者以 async/await 語法來編寫並行 (concurrent) 程式碼的函式庫 (library)。

asyncio 作為多個 Python 非同步框架的基礎,在高效能網路與網頁伺服器、資料庫連線函式庫、分散式任務佇列等服務都可以看得到它。

asyncio 往往是個建構 IO 密集型與高階層結構化網路程式碼的完美選擇。

asyncio 提供了一系列高階 API:

此外,還有一些給函式庫與框架 (framework) 開發者低階 API:

你能在 REPL 中對一個 asyncio 的並行情境 (context) 進行實驗:

$ 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-emscriptenwasm32-wasi 上不起作用或無法使用。有關更多資訊,請參閱 WebAssembly 平台

參閱

備註

asyncio 的原始碼可以在 Lib/asyncio/ 中找到。