当其中一个子进程中止和/或抛出异常时,中止多处理的正确方法是什么?
我发现了与此相关的各种问题( generic multiprocessing error handling 、 how to close multiprocessing pool on exception but without answer 、...),但没有关于如何停止子异常的多处理的明确答案。
例如,我期望以下代码:
def f(x):
sleep(x)
print(f"f({x})")
return 1.0 / (x - 2)
def main():
with Pool(4) as p:
try:
r = p.map(f, range(7))
except Exception as e:
print(f"oops: {e}")
p.close()
p.terminate()
print("end")
if __name__ == '__main__':
main()
输出:
f(0)
f(1)
f(2)
oops: float division by zero
end
相反,它在检测/处理异常之前对所有项目应用 f
函数:
f(0)
f(1)
f(2)
f(4)
f(3)
f(5)
f(6)
oops: float division by zero
end
有没有办法直接捕获异常?
最佳答案
我认为您将需要 apply_async
为此,这样您就可以对每个结果而不是累积结果采取行动。 pool.apply_async
提供了一个 error_callback
参数,您可以使用它来注册错误处理程序。 apply_async
不会阻塞,因此您需要 join()
池。我还使用标志 termerated
来了解在没有发生异常的情况下何时可以正常处理结果。
from time import sleep
from multiprocessing import Pool
def f(x):
sleep(x)
print(f"f({x})")
return 1.0 / (x - 2)
def on_error(e):
global terminated
terminated = True
pool.terminate()
print(f"oops:{e}")
def main():
global pool
global terminated
terminated = False
pool = Pool(4)
results = [pool.apply_async(f, (x,), error_callback=on_error)
for x in range(7)]
pool.close()
pool.join()
if not terminated:
for r in results:
print(r.get())
print("end")
if __name__ == '__main__':
main()
关于Python多处理: abort map on first child error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52272325/