Python 3
In Python 3, now the get method in multiprocessing.pool.Async returns a full return, see http://bugs.python.org/issue13831 .
Python 2
Use traceback.format_exc (which means formatted exception) to get the trace string. Making a decorator would be much more convenient, as shown below.
def full_traceback(func): import traceback, functools @functools.wraps(func) def wrapper(*args, **kwargs): try: return func(*args, **kwargs) except Exception as e: msg = "{}\n\nOriginal {}".format(e, traceback.format_exc()) raise type(e)(msg) return wrapper
Example:
def func0(): raise NameError("func0 exception") def func1(): return func0() # Key is here! @full_traceback def main(i): return func1() if __name__ == '__main__': from multiprocessing import Pool pool = Pool(4) try: results = pool.map_async(main, range(5)).get(1e5) finally: pool.close() pool.join()
Feedback from the decorator:
Traceback (most recent call last): File "bt.py", line 34, in <module> results = pool.map_async(main, range(5)).get(1e5) File "/opt/anaconda/lib/python2.7/multiprocessing/pool.py", line 567, in get raise self._value NameError: Exception in func0 Original Traceback (most recent call last): File "bt.py", line 13, in wrapper return func(*args, **kwargs) File "bt.py", line 27, in main return func1() File "bt.py", line 23, in func1 return func0() File "bt.py", line 20, in func0 raise NameError("Exception in func0") NameError: Exception in func0
Trace without decorator:
Traceback (most recent call last): File "bt.py", line 34, in <module> results = pool.map_async(main, range(5)).get(1e5) File "/opt/anaconda/lib/python2.7/multiprocessing/pool.py", line 567, in get raise self._value NameError: Exception in func0
Syrtis Major Apr 05 '17 at 6:29 2017-04-05 06:29
source share