[英]How to debug multithreaded python script
I have a python script which runs few threads.我有一个运行几个线程的 python 脚本。 Usually, when I want to debug a python script I run it with "-m pdb" and then setup a break point inside with "b ".通常,当我想调试 python 脚本时,我用“-m pdb”运行它,然后用“b”在里面设置一个断点。 However, for some reason it doesn't stop at the break point even it passed through that line and even I saw the break point was actually added.但是,由于某种原因,即使它通过了那条线,它也不会停在断点处,甚至我也看到实际上添加了断点。 Any idea what I'm doing wrong?知道我做错了什么吗? I used a simple example for python threading module from here我从这里为 python 线程模块使用了一个简单的例子
import threading
class SummingThread(threading.Thread):
def __init__(self,low,high):
threading.Thread.__init__(self)
self.low=low
self.high=high
self.total=0
def run(self):
print 'self.low = ' + str(self.low) + ', self.high = ' + str(self.high)
for i in range(self.low,self.high):
self.total+=i
thread1 = SummingThread(0,500000)
thread2 = SummingThread(500000,1000000)
thread1.start() # This actually causes the thread to run
thread2.start()
thread1.join() # This waits until the thread has completed
thread2.join()
# At this point, both threads have completed
result = thread1.total + thread2.total
print result
Then I add a break point inside the run
method on the line with the print
command and run the script.然后,我在run
方法中的print
命令行上添加一个断点并运行脚本。 The script runs, passes through the print
command but doesn't stop.脚本运行,通过print
命令但不会停止。
~$ python -m pdb test.py
> /home/user/test.py(1)<module>()
-> import threading
(Pdb) b 11
Breakpoint 1 at /home/user/test.py:11
(Pdb) r
self.low = 0, self.high = 500000
self.low = 500000, self.high = 1000000
499999500000
--Return--
> /home/user/test.py(24)<module>()->None
-> print result
(Pdb) q
As mentioned in the comments, pdb does not support threading.如评论中所述,pdb 不支持线程。 But you can use pdb in the threads.但是您可以在线程中使用 pdb。
import pdb
import threading
class SummingThread(threading.Thread):
...
def run(self):
# TODO: do for only one thread.
pdb.set_trace()
print 'self.low = ' + str(self.low) + ', self.high = ' + str(self.high)
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.