13

I have a python script which runs few threads. Usually, when I want to debug a python script I run it with "-m pdb" and then setup a break point inside with "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

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. The script runs, passes through the print command but doesn't stop.

~$ 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
Community
  • 1
  • 1
e271p314
  • 3,841
  • 7
  • 36
  • 61
  • 1
    Can we see some code? – turnt Feb 12 '14 at 17:28
  • 1
    This [page][1] and [this][2] maybe usefull for debugging multithread programs [1]: http://stackoverflow.com/questions/28108851/catching-segfault-with-debugger-in-python/28169186#28169186 [2]: http://stackoverflow.com/questions/1623039/python-debugging-tips – M.javid Jan 27 '15 at 11:25
  • 1
    same topic, see https://stackoverflow.com/questions/63767220/pdb-doesnt-stop-on-breakpoint-in-multithreaded-code – gftea Mar 22 '22 at 14:41

1 Answers1

0

As mentioned in the comments, pdb does not support threading. But you can use pdb in the threads.

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)
         ...         

altunyurt
  • 2,821
  • 3
  • 38
  • 53