Pdb can't break in another thread?

Consider this multithreaded program:

import threading class SomeThread(threading.Thread): def run(self): a = 1 print a def main(): print 'hola' someThread = SomeThread() someThread.start() if __name__ == '__main__': main() 

When I debug this program using pdb, at the prompt I first set a breakpoint in each of the two print statements. Then I continue. pdb is split into print 'hola' . I continue again and see the print effect in another thread, but pdb will not break.

Help commands don't list anything for switching thread contexts like gdb ... so ... is it simply impossible to set a breakpoint in one thread context that will fire in another context?

+8
python multithreading breakpoints pdb
source share
2 answers

This works for me:

 import threading import pdb class SomeThread(threading.Thread): def run(self): a = 1 print a pdb.set_trace() def main(): print 'hola' pdb.set_trace() someThread = SomeThread() someThread.start() if __name__ == '__main__': main() 

What gives me:

 C:\Code>python b.py hola > c:\code\b.py(13)main() -> someThread = SomeThread() (Pdb) l 8 pdb.set_trace() 9 10 def main(): 11 print 'hola' 12 pdb.set_trace() 13 -> someThread = SomeThread() 14 someThread.start() 15 16 if __name__ == '__main__': 17 main() [EOF] (Pdb) c 1 --Return-- > c:\code\b.py(8)run()->None -> pdb.set_trace() (Pdb) l 3 4 class SomeThread(threading.Thread): 5 def run(self): 6 a = 1 7 print a 8 -> pdb.set_trace() 9 10 def main(): 11 print 'hola' 12 pdb.set_trace() 13 someThread = SomeThread() (Pdb) 

This is under Windows 7 and with Python 2.7.2. What version of OS and Python are you using?

+5
source share

after you hit the first breakpoint, I assume that you step with (n) the next line when you get to this line

 someThread.start() 

make sure you use step (s) and not (n). pdb commands

0
source share

All Articles