2

I'm using following trick to get debugger started on error Starting python debugger automatically on error

Any idea how to make this also work for errors that happen in newly created threads? I'm using thread pool, something like http://code.activestate.com/recipes/577187-python-thread-pool/

Community
  • 1
  • 1
Yaroslav Bulatov
  • 57,332
  • 22
  • 139
  • 197

1 Answers1

2

I'd say inject that code in the beginning of each Thread's run().

If you don't want to change that code, you could do monkeypatch it e.g. like this:

Worker.run = lambda *a: [init_pdb(), Worker.run(*a)][-1]

Or like this:

def wrapper(*a):
    # init pdb here
    Worker.run(*a)

Worker.run = wrapper

If you wanna go real hardcore, you can override threading.Thread.start, or possibly threading.Thread altogether before you import other modules, e.g.:

class DebuggedThread(threading.Thread):
    def __init__(self):
        super(DebuggedThread, self).__init__()
        self._real_run = self.run
        self.run = self._debug_run
    def _debug_run(self):
        # initialize debugger here
        self._real_run()

threading.Thread = DebuggedThread
Dima Tisnek
  • 11,241
  • 4
  • 68
  • 120