asyncio --- 非同期 I/O


asyncio は async/await 構文を使い 並行処理の コードを書くためのライブラリです。

asyncio は、高性能なネットワークとウェブサーバ、データベース接続ライブラリ、分散タスクキューなどの複数の非同期 Python フレームワークの基盤として使われています。

asyncio は多くの場合、 IOバウンドだったり高レベルの 構造化された ネットワークコードに完璧に適しています。

参考

A Conceptual Overview of asyncio

Explanation of the fundamentals of asyncio.

asyncio は次の目的で 高レベル API を提供しています:

これに加えて、 ライブラリやフレームワークの開発者 が次のことをするための 低レベル API があります:

利用可能な環境: not WASI.

このモジュールは WebAssembly では動作しないか、利用不可です。詳しくは、WebAssembly プラットフォーム を見てください。

asyncio REPL

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'

This REPL provides limited compatibility with PYTHON_BASIC_REPL. It is recommended that the default REPL is used for full functionality and the latest features.

引数無しで 監査イベント cpython.run_stdin を送出します。

バージョン 3.12.5 で変更: (3.11.10、3.10.15、3.9.20、3.8.20 も同様) 監査イベントを送出するようになりました。

バージョン 3.13 で変更: 可能な場合は PyREPL を使用するようになり、その場合は PYTHONSTARTUP も実行されます。 監査イベントを送出するようになりました。

リファレンス

注釈

asyncio のソースコードは Lib/asyncio/ にあります。