Python マルチスレッドライブラリ¶
cuncurrent.futures¶
Executor¶
処理を並列実行する仕組みとして次が用意されています。
マルチプロセス、マルチスレッドで処理を実行する仕組みとして次があります。
- ProcessPoolExecutor(別プロセスで処理を実行)
- ThreadPoolExecutor(スレッドで処理を実行、GILの制約を受ける)
- InterpreterPoolExecutor (スレッドで処理を実行、GILの制約なし、Python 3.14以降)
Future¶
別プロセス/スレッドで実行している処理の完了を非同期で待ち、結果を受け取る仕組みです。
from concurrent.futures import ProcessPoolExecutor
def task1():
# do something
def task2():
# do something
with ProcessPoolExecutor(2) as executor:
future1 = executor.submit(task1)
future2 = executor.submit(task2)
:
future1.result()