用multiprocessing.Pool处理Ctrl+C/SIGINT的正确方法是:在创建进程Pool之前,使进程忽略SIGINT。这样创建的子进程继承SIGINT处理程序。
创建Pool之后,还原父进程中的原始SIGINT处理程序。
使用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中不可用。