【发布时间】:2015-09-09 12:00:20
【问题描述】:
我有 2 个进程 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 发送一些虚拟消息,或者杀死管道以在A 中引发错误,但这似乎不是很干净。
【问题讨论】:
标签: python python-2.7 exception-handling multiprocessing