When I have to debug a Python program, I generally use the interactive shell of PDB :
python -m pdb <my script>
And then set my breakpoints like this :
(Pdb) b <file>:<line>
The problem is that PDB will not break if the breakpoints are in a thread other than the main thread. For instance, in the following script :
import threading
event_quit = threading.Event()
class myThread (threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
def run(self):
print("myThread start")
print("Breakpoint here")
print("myThread end")
event_quit.set()
thread = myThread()
thread.start()
event_quit.wait()
PDB will not break if I set a breakpoint on print("Breakpoint here")
.
The solution suggested here is to put a pdb.set_trace()
where I want to break in the code , instead of setting breakpoints through the interactive shell.
I find that solution a bit clumsy (requires you to open and edit the file you want to debug) and even potentially dangerous (can cause mayhem if a pdb.set_trace()
is somehow left in production code).
Is there any way to debug multithreaded programs with PDB, without editing the files and using only the interactive shell ?