在multiprocessing中有error_callback.在Python 2中有池apply_async吗? [英] error_callback in multiprocessing.Pool apply_async in Python 2?

查看:838
本文介绍了在multiprocessing中有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.

这篇关于在multiprocessing中有error_callback.在Python 2中有池apply_async吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆