Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use ipdb.set_trace in a forked process

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.

like image 565
Seanny123 Avatar asked Oct 09 '14 22:10

Seanny123


3 Answers

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

like image 104
Yoav Glazner Avatar answered Nov 14 '22 17:11

Yoav Glazner


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

like image 43
knite Avatar answered Nov 14 '22 19:11

knite


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

like image 26
CentAu Avatar answered Nov 14 '22 17:11

CentAu