Reputation: 3591
I have 2 processes A
and B
, communicating through a multiprocessing.Pipe()
, and I would like raise an exception in B
when A
fails.
For now I have something like that:
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()
If process B
fails, there is nothing happening on A
. I wonder if there is a pythonic way to transmit the exception to A
or if I should send some dummy message through the Pipe
, or kill the Pipe to raise an Error in A
, but that doesn't seem very clean.
Upvotes: 4
Views: 910
Reputation: 7930
AFAIK you need to send your own message through the pipe. It seems like you
want to send exception from B
to A
. The code in B
for exception
handling could be like this:
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))
...
In A
:
while True:
..
data_recv = a.recv()
if isinstance(data_recv, RemoteException):
raise data_recv.error_string, None, data_recv.tb
Of course both A
and B
processes should share the same RemoteException
class.
Upvotes: 5