简述网站的建设方案,2021安全员证报名入口,网站建设维护合同书,wordpress 说说用multiprocessing.Pool处理CtrlC/SIGINT的正确方法是#xff1a;在创建进程Pool之前#xff0c;使进程忽略SIGINT。这样创建的子进程继承SIGINT处理程序。创建Pool之后#xff0c;还原父进程中的原始SIGINT处理程序。使用map_async和apply_async而不是阻塞map和apply。等待…用multiprocessing.Pool处理CtrlC/SIGINT的正确方法是在创建进程Pool之前使进程忽略SIGINT。这样创建的子进程继承SIGINT处理程序。创建Pool之后还原父进程中的原始SIGINT处理程序。使用map_async和apply_async而不是阻塞map和apply。等待超时的结果因为默认的阻塞等待忽略所有信号。这是Python错误https://bugs.python.org/issue8296。总而言之#!/bin/env pythonfrom __future__ import print_functionimport multiprocessingimport osimport signalimport timedef 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中不可用。