|
1 | 1 | import multiprocessing
|
2 | 2 | import multiprocessing.sharedctypes
|
3 |
| -import sys |
4 | 3 | import ctypes
|
5 | 4 | import time
|
6 | 5 | import logging
|
7 | 6 | from collections import namedtuple
|
| 7 | +import traceback |
8 | 8 |
|
9 | 9 | import six
|
10 | 10 | import numpy as np
|
|
13 | 13 |
|
14 | 14 | logger = logging.getLogger('pymc3')
|
15 | 15 |
|
| 16 | + |
| 17 | +# Taken from https://hg.python.org/cpython/rev/c4f92b597074 |
| 18 | +class RemoteTraceback(Exception): |
| 19 | + def __init__(self, tb): |
| 20 | + self.tb = tb |
| 21 | + |
| 22 | + def __str__(self): |
| 23 | + return self.tb |
| 24 | + |
| 25 | + |
| 26 | +class ExceptionWithTraceback: |
| 27 | + def __init__(self, exc, tb): |
| 28 | + tb = traceback.format_exception(type(exc), exc, tb) |
| 29 | + tb = ''.join(tb) |
| 30 | + self.exc = exc |
| 31 | + self.tb = '\n"""\n%s"""' % tb |
| 32 | + |
| 33 | + def __reduce__(self): |
| 34 | + return rebuild_exc, (self.exc, self.tb) |
| 35 | + |
| 36 | + |
| 37 | +def rebuild_exc(exc, tb): |
| 38 | + exc.__cause__ = RemoteTraceback(tb) |
| 39 | + return exc |
| 40 | + |
| 41 | + |
16 | 42 | # Messages
|
17 | 43 | # ('writing_done', is_last, sample_idx, tuning, stats)
|
18 | 44 | # ('error', *exception_info)
|
@@ -47,9 +73,9 @@ def run(self):
|
47 | 73 | self._start_loop()
|
48 | 74 | except KeyboardInterrupt:
|
49 | 75 | pass
|
50 |
| - except BaseException: |
51 |
| - exc_info = sys.exc_info() |
52 |
| - self._msg_pipe.send(('error', exc_info[:2])) |
| 76 | + except BaseException as e: |
| 77 | + e = ExceptionWithTraceback(e, e.__traceback__) |
| 78 | + self._msg_pipe.send(('error', e)) |
53 | 79 | finally:
|
54 | 80 | self._msg_pipe.close()
|
55 | 81 |
|
@@ -193,7 +219,7 @@ def recv_draw(processes, timeout=3600):
|
193 | 219 | msg = ready[0].recv()
|
194 | 220 |
|
195 | 221 | if msg[0] == 'error':
|
196 |
| - old = msg[1][1]#.with_traceback(msg[1][2]) |
| 222 | + old = msg[1] |
197 | 223 | six.raise_from(RuntimeError('Chain %s failed.' % proc.chain), old)
|
198 | 224 | elif msg[0] == 'writing_done':
|
199 | 225 | proc._readable = True
|
|
0 commit comments