python - 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
from 中的语句thread1
正在干扰thread2
.
在 PyCharm 的调试器中,可以暂停所有线程:PyCharm - 如何暂停所有线程
是否可以暂停 PDB 中的所有线程?
解决方案
目前不支持。调试器pdb
被描述为不适合调试多线程应用程序。
- 问题 21281 - 这是一个 6 年前的增强请求,以支持在触发断点时停止所有线程。它没有受到太多关注。
- 问题 41571 - 这是最近的增强请求,以向 pdb 添加更好的线程支持。
- Python Wiki 中的PythonDebugTools页面列出了支持调试多线程应用程序的调试器和 IDE。
推荐阅读
- ios - 类型“UIView”没有成员“layoutFittingCompressedSize”
- dynamics-crm - 在调查响应中填充联系人和机会查找
- ios - 给定四边形的四个角位置,填充四边形之外的所有内容
- python - Azure 存储关闭自动解压
- android - 如何连续读取logcat并写入内部存储文件?
- python - 带有 tf 数据集输入的 TensorFlow keras
- javascript - Google 电子邮件脚本更改日期
- jquery - jQuery .closest() 在 AJAX 调用后不起作用?
- oracle - 在 toad 中,exact fetch 返回的行数超过了请求的行数
- git - 将主分支重置为功能分支