Python:从multiprocessing.Process获取回溯

Python:从multiprocessing.Process获取回溯,第1张

Python:从multiprocessing.Process获取回溯

使用

tblib
您可以传递包装的异常并在以后重新引发它们:

import tblib.pickling_supporttblib.pickling_support.install()from multiprocessing import Poolimport sysclass ExceptionWrapper(object):    def __init__(self, ee):        self.ee = ee        __, __, self.tb = sys.exc_info()    def re_raise(self):        raise self.ee.with_traceback(self.tb)        # for Python 2 replace the previous line by:        # raise self.ee, None, self.tb# example of how to use ExceptionWrapperdef inverse(i):    """ will fail for i == 0 """    try:        return 1.0 / i    except Exception as e:        return ExceptionWrapper(e)def main():    p = Pool(1)    results = p.map(inverse, [0, 1, 2, 3])    for result in results:        if isinstance(result, ExceptionWrapper): result.re_raise()if __name__ == "__main__":    main()

因此,如果您在远程进程中捕获到异常,则将其包装

ExceptionWrapper
,然后将其传递回去。调用
re_raise()
主流程即可完成工作。



欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5630111.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-12-15
下一篇2022-12-15

发表评论

登录后才能评论

评论列表(0条)

    保存