How to use ipdb.set_trace in a forked process
Asked Answered
J

3

24

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.

Jacquejacquelin answered 9/10, 2014 at 22:43 Comment(3)
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?.Endoskeleton
@Endoskeleton I have confirmed it does work, but I (personally) don't feel it answers my questionJacquejacquelin
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.Endoskeleton
M
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).

Museum answered 19/2, 2016 at 7:2 Comment(1)
Did you try WinPDB on a python3 application? It seems to me that it's useful only to debug python2 programsEckardt
S
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...

Selway answered 14/2, 2016 at 21:19 Comment(0)
M
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).

Museum answered 19/2, 2016 at 7:2 Comment(1)
Did you try WinPDB on a python3 application? It seems to me that it's useful only to debug python2 programsEckardt
P
3

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://mcmap.net/q/169415/-how-to-attach-debugger-to-a-python-subproccess

Pinson answered 17/3, 2022 at 18:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.