PDB-如何暂停所有线程

问题描述

当多线程Python程序遇到断点时,相关线程将停止,但其他线程将继续运行。在某些情况下,这可能是调试的障碍。

例如,在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()

将导致以下调试会话:

$ python test.py 
--Return--
> /.../test.py(12)thread2()->None
-> breakpoint()
(Pdb) hello
hello
hello
hello
...

如您所见,来自print的{​​{1}}语句正在干扰thread1中的调试会话。

在PyCharm的调试器中,可以挂起所有线程:PyCharm - how to suspend all threads

是否可以挂起PDB中的所有线程?

解决方法

当前不受支持。没有将pdb调试器描述为调试多线程应用程序的好方法。

  • Issue 21281-这是6年的增强请求,以支持在触发断点时停止所有线程。它并没有引起太多关注。
  • Issue 41571-这是最近的一项增强请求,旨在为pdb添加更好的线程支持。
  • Python Wiki中的PythonDebugTools页面列出了支持调试多线程应用程序的调试器和IDE。