|
发表于 2022-4-2 13:57:43
|
显示全部楼层
没有尝试,不过知乎有个代码可以试一试。
https://www.zhihu.com/question/52776465/answer/1492100420
python捕获ctrl+c事件,然后再事件里把所有子进程kill掉。
- 作者:黄哥
- 链接:https://www.zhihu.com/question/52776465/answer/132004906
- 来源:知乎
- 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
- def terminate_all(self):
- with self._lock:
- for p in self.procs:
- if p.is_alive():
- print "Terminating %s" % p
- p.terminate()
- def launch_proc(self, func, args=(), kwargs= {}):
- t = threading.Thread(target=self._proc_thread_runner,
- args=(func, args, kwargs))
- self._threads.append(t)
- t.start()
- def _proc_thread_runner(self, func, args, kwargs):
- p = multiprocessing.Process(target=func, args=args, kwargs=kwargs)
- self.procs.append(p)
- p.start()
- while p.exitcode is None:
- p.join()
- if p.exitcode > 0:
- self.errors_flag = True
- self.terminate_all()
复制代码 |
|