Can I put break points on background threads in Python?

后端 未结 4 1011
無奈伤痛
無奈伤痛 2021-01-12 08:04

I\'m using the PyDev for Eclipse plugin, and I\'m trying to set a break point in some code that gets run in a background thread. The break point never gets hit even though t

4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-12 08:48

    The underlying issue is with sys.settrace, the low-level Python function used to perform all tracing and debugging -- as the docs say,

    The function is thread-specific; for a debugger to support multiple threads, it must be registered using settrace() for each thread being debugged.

    I believe that when you set a breakpoint in PyDev, the resulting settrace call is always happening on the main thread (I have not looked at PyDev recently so they may have added some way to work around that, but I don't recall any from the time when I did look).

    A workaround you might implement yourself is, in your main thread after the breakpoint has been set, to use sys.gettrace to get PyDev's trace function, save it in a global variable, and make sure in all threads of interest to call sys.settrace with that global variable as the argument -- a tad cumbersome (more so for threads that already exist at the time the breakpoint is set!), but I can't think of any simpler alternative.

提交回复
热议问题