python 回溯_Python:从多处理进程中获取回溯

I am trying to get hold of a traceback object from a multiprocessing.Process.

Unfortunately passing the exception info through a pipe does not work because traceback objects can not be pickled:

def foo(pipe_to_parent):

try:

raise Exception('xxx')

except:

pipe_to_parent.send(sys.exc_info())

to_child, to_self = multiprocessing.Pipe()

process = multiprocessing.Process(target = foo, args = (to_self,))

process.start()

exc_info = to_child.recv()

process.join()

print traceback.format_exception(*exc_info)

to_child.close()

to_self.close()

Traceback:

Traceback (most recent call last):

File "/usr/lib/python2.6/multiprocessing/process.py", line 231, in _bootstrap

self.run()

File "/usr/lib/python2.6/multiprocessing/process.py", line 88, in run

self._target(*self._args, **self._kwargs)

File "foo", line 7, in foo

to_parent.send(sys.exc_info())

PicklingError: Can't pickle : attribute lookup __builtin__.traceback failed

Is there another way to access the exception info? I'd like to avoid passing the formatted string.

解决方案

Using tblib you can pass wrapped exceptions and reraise them later:

import tblib.pickling_support

tblib.pickling_support.install()

from multiprocessing import Pool

import sys

class 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 how to use ExceptionWrapper

def 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()

So, if you catch an exception in your remote process, wrap it with ExceptionWrapper and then pass it back. Calling re_reraise in the main process will do the work.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值