multiprocessing.Pool
类的函数 apply_async
在 Python 3 中有参数 error_callback
。但是 Python 2 中缺少此参数。
有什么技巧可以在 Python 2 中实现相同的功能吗?理想情况下,我想编写在 Python 2 和 3 中运行的代码。
最佳答案
我还没有尝试过 python3。但对我来说,为了捕获子进程中的错误,我将在子进程中运行的函数放在
import traceback
try:
your code that can make error
except Exception as e:
print e
return False, traceback.format_exc()
else:
return True, result
这样我就能知道是否出了问题。
编辑:我将返回格式更改为 OP 的注释,以便子进程返回一个元组 (is_success, result or error traceback message)
因此主进程将首先读取标志 is_success
,然后相应地处理第二个参数。
关于python - multiprocessing.Pool apply_async 中的 error_callback 在 Python 2 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27986885/