我有两个进程A
和B
,它们通过一个multiprocessing.Pipe()
进行通信。当A
失败时,我想在B
中引发异常。
目前我的代码大致如下:
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
中引发错误,但这似乎不太干净。
RemoteException
对象吧?我可以直接发送e
并使用你的if isinstance(e,Exception):
代码在A
中抛出异常。明天我会试一下。感谢你的帮助! - CoMartel