Python多处理:出现错误后如何干净退出? [英] Python multiprocessing: how to exit cleanly after an error?

查看:273
本文介绍了Python多处理:出现错误后如何干净退出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一些使用multiprocessing模块的代码.但是,由于我是新手,所以经常发生的是会弹出一些错误,使主应用程序停止工作.

I am writing some code that makes use of the multiprocessing module. However, since I am a newbie, what often happens is that some error pops up, putting a halt to the main application.

但是,这些应用程序的子级仍然保持运行状态,并且在任务管理器列表中,我可以看到很长的正在运行的pythonw进程列表.

However, that applications' children still remain running, and I get a long, long list of running pythonw processes in my task manager list.

发生错误后,我该怎么做以确保所有子进程也被杀死?

After an error occurs, what can I do to make sure all the child processes are killed as well?

推荐答案

此难题有两部分.

  1. 如何检测并杀死所有子进程?
  2. 当一个进程死亡时,我如何尽最大努力确保我的第1部分中的代码能够运行?

对于第1部分,您可以使用 multiprocessing.active_children() 来获取所有活动孩子的列表,并使用Process.terminate()杀死他们.请注意,Process.terminate()的使用带有通常的警告.

For part 1, you can use multiprocessing.active_children() to get a list of all the active children and kill them with Process.terminate(). Note the use of Process.terminate() comes with the usual warnings.

from multiprocessing import Process
import multiprocessing

def f(name):
    print 'hello', name
    while True: pass

if __name__ == '__main__':
    for i in xrange(5):
        p = Process(target=f, args=('bob',))
        p.start()

    # At user input, terminate all processes.
    raw_input("Press Enter to terminate: ")
    for p in multiprocessing.active_children():
       p.terminate()

第2部分的一种解决方案是使用sys.excepthook,如此答案所述.这是一个组合的示例.

One solution to part 2 is to use sys.excepthook, as described in this answer. Here is a combined example.

from multiprocessing import Process
import multiprocessing
import sys
from  time import sleep

def f(name):
    print 'hello', name
    while True: pass

def myexcepthook(exctype, value, traceback):
    for p in multiprocessing.active_children():
       p.terminate()

if __name__ == '__main__':
    for i in xrange(5):
        p = Process(target=f, args=('bob',))
        p.start()
    sys.excepthook = myexcepthook

    # Sleep for a bit and then force an exception by doing something stupid.
    sleep(1)
    1 / 0

这篇关于Python多处理:出现错误后如何干净退出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆