LoginSignup
14
15

More than 5 years have passed since last update.

Pythonの async/await, asyncio の使い道

Last updated at Posted at 2018-10-20

Pythonの async/await, asyncio の使い道

Pythonのasync/awaitは、C#などと違って、全部一つのスレッド上でやるので、CPUバウンドな処理や、大きなファイルを保存するようなI/Oバウンドな処理を速くするのは無理。

通信を待ってる自分が何もしてない時間に他の処理をやりたいようなときに使える。

CPUバウンドなときはmultiprocessingやconcurrent.futures.ProccessPoolExecutorを使うべし

I/Oバウンドなときはconcurrent.futures.ThreadPoolExecutorを使うべき

async await

このブログがわかりやすい
ゆくゆくは有へと: Pythonの非同期通信(asyncioモジュール)入門を書きました

async awaitでマルチプロセスやマルチスレッドを使う

loop.run_in_executorを使えば、concurrent.futures.ThreadPoolExecutorに渡した関数の完了をawaitすることができる。
Executing code in thread or process pools

14
15
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
14
15