When a multithreaded Python program hits a breakpoint, the relevant thread will stop but the other threads will continue running. In some cases, this can be an obstacle to debugging.
For example, in test.py
:
from threading import Thread
from time import sleep
def thread1():
while True:
sleep(1)
print("hello")
def thread2():
breakpoint()
Thread(target=thread1).start()
Thread(target=thread2).start()
Will lead to the following debugging session:
$ python test.py
--Return--
> /.../test.py(12)thread2()->None
-> breakpoint()
(Pdb) hello
hello
hello
hello
...
As you can see, the print
statement from thread1
is disturbing the debugging session in thread2
.
In PyCharm's debugger, it's possible to suspend all threads: PyCharm - how to suspend all threads
Is it possible to suspend all threads in PDB?