【发布时间】: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
...
如您所见,来自thread1 的print 语句正在干扰thread2 中的调试会话。
在 PyCharm 的调试器中,可以暂停所有线程:PyCharm - how to suspend all threads
是否可以暂停 PDB 中的所有线程?
【问题讨论】:
标签: python multithreading debugging pdb