多处理中的error_callback.Python 2中的池apply_async?
问题描述
函数apply_async
在Python 3 中具有参数error_callback
.但是Python 2 中没有这个论点.
Function apply_async
of multiprocessing.Pool
class has argument error_callback
in Python 3. But this argument is missing in Python 2.
有什么技巧可以在 Python 2 中实现相同的功能吗?理想情况下,我想编写在 Python 2 和 3 中运行的代码.
Is there any trick to achieve the same functionality in Python 2 ? Ideally I would like to write code which runs in both Python 2 and 3.
解决方案
我还没试过python3.但对我来说,为了捕捉子进程中的错误,我将在子进程中运行的函数放在一个
I haven't tried python3 yet. But for me, to catch the errors in the child process, I put the function that runs in child process within a
import traceback
try:
your code that can make error
except Exception as e:
print e
return False, traceback.format_exc()
else:
return True, result
这样我会知道是否有问题.
So that I will know if something goes wrong.
我将返回格式更改为 OP 的注释,以便子进程返回一个元组 (is_success, result or error traceback message)
I change the return format as OP's comment so that the child process returns a tuple (is_success, result or error traceback message )
这样主进程将首先读取标志is_success
,然后相应地处理第二个参数.
So that main process will first read the flag is_success
and then handles the second argument accordingly.
相关文章