23

I use ipdb.set_trace() whenever I need to set a break point in my code. Right now, I'm trying to use it in a process that I've created using multiprocessing, while the code does stop, I can't type anything to continue debugging. Is there any way to get my stdin directed properly?

Ideally, I would like to imagine a new console opening everytime a forked process is stopped for debugging, however I don't think this is possible.

Seanny123
  • 8,776
  • 13
  • 68
  • 124
  • 1
    It would be nice if you verify the answer given by @yoav-glazner. Are you able to use your keyboard after changing _multiprocessing_ by _dummy_?. – jgomo3 Feb 15 '16 at 15:08
  • 3
    @jgomo3 I have confirmed it does work, but I (personally) don't feel it answers my question – Seanny123 Feb 15 '16 at 15:27
  • I suppose for the fact that you had to change te code every time you want to debug. One could argue you do it already with pdb.set_trace(), but I know you fell there should be another way to achieve the same without changing the code. I wonder if is there a way to tell pdb to use dummy instead of multiprocessing for the multiprocessing import? A kind of monkeypatching/mocking for debugging. – jgomo3 Feb 16 '16 at 16:27

3 Answers3

12

Sometimes for debugging You can change your code to use multiprocessing.dummy . This way, no fork will be done, it will work with threads and be easier to debug.

Later on (after the bug is squashed...) you can switch back to multiprocessing

multiprocessing.dummy - should offer the same API as multiprocessing so its an easy change...

Yoav Glazner
  • 7,936
  • 1
  • 19
  • 36
4

According to How to attach debugger to a python subproccess?, http://winpdb.org supports multiprocessing debugging.

If you'd prefer doing more work for more flexibility, there are some interesting ideas at https://gist.github.com/csabahenk/6497709 (too long to include here).

Community
  • 1
  • 1
knite
  • 6,033
  • 6
  • 38
  • 54
  • Did you try WinPDB on a python3 application? It seems to me that it's useful only to debug python2 programs – fstab Aug 02 '16 at 09:46
2

Instead of pdb.set_trace() use the following:

import sys
import pdb

class ForkedPdb(pdb.Pdb):
    """A Pdb subclass that may be used
    from a forked multiprocessing child

    """
    def interaction(self, *args, **kwargs):
        _stdin = sys.stdin
        try:
            sys.stdin = open('/dev/stdin')
            pdb.Pdb.interaction(self, *args, **kwargs)
        finally:
            sys.stdin = _stdin

ForkedPdb().set_trace()

Source: https://stackoverflow.com/a/23654936/3450064

CentAu
  • 10,660
  • 15
  • 59
  • 85