在多进程中处理异常

时间:2015-09-09 12:00:20

标签: python python-2.7 exception-handling multiprocessing

我有2个进程AB,通过multiprocessing.Pipe()进行通信,当B失败时,我想在A中引发异常。 现在我有类似的东西:

def A_function():
    try:
        a,b=Pipe()
        B=Process(target=B_function,args=(b,))
        B.start()
        while True:
            a.send(data)
            data_recv=a.recv()
    except Exception as e:
        print e
        #  terminate process properly

def B_function(b):
    try:
        while True:
            data_recv=b.recv()
            # do some work on data_recv, but can fail
            b.send(modified_data)
    except Exception as e:
        print e
        raise # not working on the other process `A`

A=Process(target=A_function)
A.start()

如果流程B失败,A上就不会发生任何事情。我想知道是否有一种pythonic方式将异常传递给A,或者我是否应该通过Pipe发送一些虚拟消息,或者杀死Pipe以在A中引发错误,但是这似乎不是很干净。

1 个答案:

答案 0 :(得分:3)

AFAIK您需要通过管道发送自己的消息。看起来像你 想要将B的异常发送到AB中的代码用于例外 处理可能是这样的:

class RemoteException(object):
    def __init__(self, exc, err_string, tb):
        self.exception = exc
        self.error_string = err_string
        self.tb = tb

try:
    data_recv = b.recv()
except Exception:
    exception, error_string, tb = sys.exc_info()
    b.send(RemoteException(exception, error_string, tb))
    ...

A

while True:
    ..
    data_recv = a.recv()
    if isinstance(data_recv, RemoteException):
        raise data_recv.error_string, None, data_recv.tb

当然,AB个进程应该共享相同的RemoteException类。