我有以下代码,使用concurrent.futures.ThreadPoolExecutor以计量的方式启动另一个程序的进程(最多30个)。 我还想要能够在按下ctrl-C停止python进程时停止所有工作。此代码有效,但有一个注意点:我必须按两次ctrl-C。第一次发送SIGINT时,没有任何反应;第二次,我看到“向进程发送SIGKILL”,进程死亡,并且它起作用了。我的第一个SIGINT发生了什么?
execution_list = [['prog', 'arg1'], ['prog', 'arg2']] ... etc
processes = []
def launch_instance(args):
process = subprocess.Popen(args)
processes.append(process)
process.wait()
try:
with concurrent.futures.ThreadPoolExecutor(max_workers=30) as executor:
results = list(executor.map(launch_instance, execution_list))
except KeyboardInterrupt:
print('sending SIGKILL to processes')
for p in processes:
if p.poll() is None: #If process is still alive
p.send_signal(signal.SIGKILL)