最終更新:2021-01-21 (木) 04:04:30 (1185d)  

Python/並列処理
Top / Python / 並列処理

メモ

  • GILの制約により、 1つのPythonインタープリタでは同時に1つのスレッドしかコードを実行できない。 したがってCPUバウンドなピュアPythonコードを Python/threading でマルチスレッド化しても速くならない。
  • 一方Python/multiprocessingは新しいインタプリタをos.fork() で立ち上げるので、 CPUバウンドなPythonコードもGILに邪魔されず並列処理できる。 ただし通信のため関数や返り値がpicklableでなければならない。 (UnixWindows)
  • それらの低級ライブラリを使いやすくまとめたのが Python/concurrent.futures (Python 3.2~) なので、とりあえずこれを使えばよい。

Python/threading

Python/multiprocessing

  • プロセスベースの並列処理

spawn

  • The parent process starts a fresh python interpreter process. The child process will only inherit those resources necessary to run the process object's run?() method.

fork

forkserver?

Python/concurrent.futures

Python/concurrent.futures.ThreadPoolExecutor

Python/concurrent.futures.ProcessPoolExecutor

参考