Noé Rubinstein
Noé Rubinstein

Reputation: 940

PDB - How to suspend all threads

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?

Upvotes: 2

Views: 3391

Answers (1)

Stephen C
Stephen C

Reputation: 719689

It isn't currently supported. The pdb debugger is not described as being good for debugging multi-threaded applications.

  • Issue 21281 - this is a 6 year old request for enhancement to support stopping all threads when a breakpoint is triggered. It hasn't received much attention.
  • Issue 41571 - this is a more recent request for enhancement to add better thread support to pdb.
  • The PythonDebugTools page in the Python Wiki lists debuggers and IDEs that support debugging of multi-threaded applications.

Upvotes: 7

Related Questions