error_callback in multiprocessing.Pool apply_async in Python 2?(多处理中的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.
这篇关于多处理中的error_callback.Python 2中的池apply_async?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:多处理中的error_callback.Python 2中的池apply_async?
基础教程推荐
- kivy 应用程序中的一个简单网页作为小部件 2022-01-01
- Python,确定字符串是否应转换为 Int 或 Float 2022-01-01
- matplotlib 设置 yaxis 标签大小 2022-01-01
- Python 中是否有任何支持将长字符串转储为块文字或折叠块的 yaml 库? 2022-01-01
- Kivy 使用 opencv.调整图像大小 2022-01-01
- 在 Python 中将货币解析为数字 2022-01-01
- 究竟什么是“容器"?在蟒蛇?(以及所有的 python 容器类型是什么?) 2022-01-01
- 在 Django Admin 中使用内联 OneToOneField 2022-01-01
- 比较两个文本文件以找出差异并将它们输出到新的文本文件 2022-01-01
- 对多索引数据帧的列进行排序 2022-01-01
