以前に受け入れられたソリューションmap
には競合状態があり、機能しませんasync
。
Ctrl+C/SIGINT
を処理する正しい方法multiprocessing.Pool
は次のとおりです。
- プロセスが作成さ
SIGINT
れる前にプロセスを無視させます。このようにして作成された子プロセスは、ハンドラーPool
を継承します。SIGINT
- が作成された
SIGINT
後、親プロセスで元のハンドラを復元します。Pool
map_async
とapply_async
をブロックする代わりにmap
とを使用しapply
ます。
- デフォルトのブロッキングはすべてのシグナルを無視するために待機するため、タイムアウトで結果を待機します。これは Python バグhttps://bugs.python.org/issue8296です。
それを一緒に入れて:
#!/bin/env python
from __future__ import print_function
import multiprocessing
import os
import signal
import time
def run_worker(delay):
print("In a worker process", os.getpid())
time.sleep(delay)
def main():
print("Initializng 2 workers")
original_sigint_handler = signal.signal(signal.SIGINT, signal.SIG_IGN)
pool = multiprocessing.Pool(2)
signal.signal(signal.SIGINT, original_sigint_handler)
try:
print("Starting 2 jobs of 5 seconds each")
res = pool.map_async(run_worker, [5, 5])
print("Waiting for results")
res.get(60) # Without the timeout this blocking call ignores all signals.
except KeyboardInterrupt:
print("Caught KeyboardInterrupt, terminating workers")
pool.terminate()
else:
print("Normal termination")
pool.close()
pool.join()
if __name__ == "__main__":
main()
@YakovShklarovが指摘したように、親プロセスでシグナルを無視してから無視を解除するまでの間に時間枠があり、その間にシグナルが失われる可能性があります。代わりに使用pthread_sigmask
して、親プロセスでシグナルの配信を一時的にブロックすると、シグナルが失われるのを防ぐことができますが、Python-2 では利用できません。