首页 > 解决方案 > 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
...

如您所见,printfrom 中的语句thread1正在干扰thread2.

在 PyCharm 的调试器中,可以暂停所有线程:PyCharm - 如何暂停所有线程

是否可以暂停 PDB 中的所有线程?

标签: pythonmultithreadingdebuggingpdb

解决方案


目前不支持。调试器pdb被描述为不适合调试多线程应用程序。

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

推荐阅读