多处理中的error_callback.Python 2中的池apply_async?

error_callback in multiprocessing.Pool apply_async in Python 2?(多处理中的error_callback.Python 2中的池apply_async?)
本文介绍了多处理中的error_callback.Python 2中的池apply_async?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

函数apply_asyncPython 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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

groupby multiple coords along a single dimension in xarray(在xarray中按单个维度的多个坐标分组)
Group by and Sum in Pandas without losing columns(Pandas中的GROUP BY AND SUM不丢失列)
Group by + New Column + Grab value former row based on conditionals(GROUP BY+新列+基于条件的前一行抓取值)
Groupby and interpolate in Pandas(PANDA中的Groupby算法和插值算法)
Pandas - Group Rows based on a column and replace NaN with non-null values(PANAS-基于列对行进行分组,并将NaN替换为非空值)
Grouping pandas DataFrame by 10 minute intervals(按10分钟间隔对 pandas 数据帧进行分组)