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.
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...
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).
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With