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/

4

1 回答 1

2

我会说在每个线程的 run() 开头注入该代码。

如果您不想更改该代码,则可以对其进行monkeypatch,例如:

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

或者像这样:

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

Worker.run = wrapper

如果你想要真正的硬核,你可以在导入其他模块之前完全覆盖 threading.Thread.start,或者可能完全覆盖 threading.Thread,例如:

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
于 2012-06-12T15:58:27.523 回答