from concurrent.futures import ProcessPoolExecutor
import time
def foo(num):
time.sleep(0.5)
print(num)
if __name__ == "__main__":
while True:
with ProcessPoolExecutor(max_workers=8) as exe:
exe.map(foo, range(1,4))
我打算使用模块ProcessPoolExecutor
。在主进程启动一段时间后,我想使用linux kill命令kill -9 main_process_pid
杀死该进程。然后我发现只有主进程被杀死,而子进程仍然处于活动状态。
user 41502 41004 5 16:16 pts/202 00:00:00 python demo.py
user 42009 41502 0 16:16 pts/202 00:00:00 python demo.py
user 42010 41502 0 16:16 pts/202 00:00:00 python demo.py
user 42011 41502 0 16:16 pts/202 00:00:00 python demo.py
user 42012 41502 0 16:16 pts/202 00:00:00 python demo.py
user 42013 41502 0 16:16 pts/202 00:00:00 python demo.py
user 42015 41502 0 16:16 pts/202 00:00:00 python demo.py
user 42025 41502 0 16:16 pts/202 00:00:00 python demo.py
user 42026 41502 0 16:16 pts/202 00:00:00 python demo.py
运行kill -9 41502
后,它变成了
user 45109 1 0 16:17 pts/202 00:00:00 python demo.py
user 45110 1 0 16:17 pts/202 00:00:00 python demo.py
user 45111 1 0 16:17 pts/202 00:00:00 python demo.py
user 45112 1 0 16:17 pts/202 00:00:00 python demo.py
user 45113 1 0 16:17 pts/202 00:00:00 python demo.py
user 45114 1 0 16:17 pts/202 00:00:00 python demo.py
user 45115 1 0 16:17 pts/202 00:00:00 python demo.py
user 45116 1 0 16:17 pts/202 00:00:00 python demo.py
我应该怎么做才能在关闭主进程时杀死所有子进程?
答案 0 :(得分:0)