How to attach debugger to a python subproccess?

前端 未结 8 2306
旧巷少年郎
旧巷少年郎 2020-12-22 23:49

I need to debug a child process spawned by multiprocessing.Process(). The pdb degugger seems to be unaware of forking and unable to attach to alrea

8条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-23 00:32

    I've been searching for a simple to solution for this problem and came up with this:

    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
    

    Use it the same way you might use the classic Pdb:

    ForkedPdb().set_trace()
    

提交回复
热议问题