提高了exception
我想跳進那個框架。爲了更好地解釋我的意思是我寫這篇兆瓦:如何從異常「跳」到堆棧幀?
假設我有以下代碼:
from multiprocessing import Pool
import sys
# Setup debugger
def raiseDebugger(*args):
""" http://code.activestate.com/recipes/65287-automatically-start-the-
debugger-on-an-exception/ """
import traceback, pdb
traceback.print_exception(*args)
pdb.pm()
sys.excepthook = raiseDebugger
# Now start with the question
def faulty(i):
return 1/i
with Pool() as pool:
pool.map(faulty, range(6))
這並不奇怪導致:
multiprocessing.pool.RemoteTraceback:
"""
Traceback (most recent call last):
File "/home/bin/conda/lib/python3.5/multiprocessing/pool.py", line 119, in worker
result = (True, func(*args, **kwds))
File "/home/bin/conda/lib/python3.5/multiprocessing/pool.py", line 44, in mapstar
return list(map(*args))
File "test2.py", line 19, in faulty
return 1/i
ZeroDivisionError: division by zero
"""
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "test2.py", line 23, in <module>
pool.map(faulty, range(6))
File "/home/bin/conda/lib/python3.5/multiprocessing/pool.py", line 260, in map
return self._map_async(func, iterable, mapstar, chunksize).get()
File "/home/bin/conda/lib/python3.5/multiprocessing/pool.py", line 608, in get
raise self._value
ZeroDivisionError: division by zero
> /home/bin/conda/lib/python3.5/multiprocessing/pool.py(608)get()
-> raise self._value
(Pdb)
我們調試,我想這個問題「跳轉」到最初引發exception
(ZeroDivisionError
)的幀中。
最初的例外仍然可以在self._value
下完成self._value.__traceback__
。
謝謝你的回答。我用我的例子嘗試過,但是'self._value .__ context__'是'None'。這是由於這樣一個事實,即異常被提出而不是重新加註(不加引發地提出)? –
哦,嗯,遠程回溯看起來像是一個字符串,但是考慮到你得到了什麼(例外是'self._value'),你可能想用'self._value .__ traceback__'來完成。對不起,我的答案沒有更清楚。 – metatoaster
我試過了。然而它在主進程中用'raise self._value'加載框架。我想這是由於多處理造成的限制。我絕對從你的答案中學到了一些東西,並會接受它,除非有人在第二天找到解決問題的辦法。 –