24

Consider this multi-threaded 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 with pdb, at the prompt I first set a break point at each of the two print statements. Then I continue. pdb breaks at print 'hola'. I continue again and see the effect of the print in the other thread, but pdb doesn't break.

The help commands don't list anything to switch thread contexts like gdb... so... is it just not possible in one thread context to set a breakpoint that will trip in another context?

Gray
  • 115,027
  • 24
  • 293
  • 354
dim fish
  • 469
  • 1
  • 4
  • 12
  • 1
    Possible duplicate of [PDB won't stop on breakpoint](https://stackoverflow.com/questions/7617066/pdb-wont-stop-on-breakpoint) – FlipMcF Jan 08 '19 at 18:19
  • 1
    Hi Flip, yes I believe this is a duplicate of that answered question. I don't see an option to mark duplicate, maybe that's an action I haven't unlocked yet. Anyone who can, please do. – dim fish Jul 17 '19 at 02:25

2 Answers2

9

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

Which 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 OS & Python version are you using?

Matthew Trevor
  • 14,354
  • 6
  • 37
  • 50
  • 8
    I think the question has to do with breakpoints set within the debugger: `(Pdb) b 8` - breakpoint on print statement. The main thread honors the breakpoint, but the child thread does not. – FlipMcF Jan 08 '19 at 17:56
-3

after you hit your first breakpoint, I assume you are stepping with (n) next line when you get to this line

someThread.start()

make sure you are using (s) step-into and not (n) next. pdb commands

Jeff Sheffield
  • 5,768
  • 3
  • 25
  • 32