【发布时间】:2021-04-26 03:31:59
【问题描述】:
我有以下代码,它使用 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)
【问题讨论】:
标签: python threadpoolexecutor concurrent.futures