【问题标题】:PDB - How to suspend all threadsPDB - 如何挂起所有线程
【发布时间】:2020-11-04 21:34:01
【问题描述】:

当多线程 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
...

如您所见,来自thread1print 语句正在干扰thread2 中的调试会话。

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

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

【问题讨论】:

    标签: python multithreading debugging pdb


    【解决方案1】:

    目前不支持。 pdb 调试器并不适合调试多线程应用程序。

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

    【讨论】:

      猜你喜欢
      • 2016-06-16
      • 2011-04-01
      • 2015-09-05
      • 1970-01-01
      • 2013-09-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多