1
2

More than 1 year has passed since last update.

asyncio、コルーチン、async/await

Last updated at Posted at 2022-04-27

asyncio

async/await 構文を使い 並行処理の コードを書くためのライブラリです
https://docs.python.org/ja/3/library/asyncio.html

では、async/awaitとは?

async/await

  • 非同期で実装する際に使用する
  • awaitは、処理の完了を待っている間にも他の処理を並行して行える
sample.py
async def edit():
    results = await get()
    return results

次の様にawaitを使わない場合、

sample.py
async def edit():
    print(get())
# > RuntimeWarning: coroutine 'SampleController.get' was never awaited

「コルーチンがawaitされていない」と。
コルーチンを単に呼び出しただけでは実行出来ず、コルーチンオブジェクトが返るため

コルーチンとは?

コルーチン

async/await 構文で宣言された関数はコルーチンと呼ばれる

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2