How to attach debugger to a python subproccess?

前端 未结 8 2280
旧巷少年郎
旧巷少年郎 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条回答
  •  梦毁少年i
    2020-12-23 00:29

    Here is the version of the ForkedPdb(Romuald's Solution) which will work for Windows and *nix based systems.

    import sys
    import pdb
    import win32console
    
    
    class MyHandle():
        def __init__(self):
            self.screenBuffer = win32console.GetStdHandle(win32console.STD_INPUT_HANDLE)
        
        def readline(self):
            return self.screenBuffer.ReadConsole(1000)
    
    class ForkedPdb(pdb.Pdb):
        def interaction(self, *args, **kwargs):
            _stdin = sys.stdin
            try:
                if sys.platform == "win32":
                    sys.stdin = MyHandle()
                else:
                    sys.stdin = open('/dev/stdin')
                pdb.Pdb.interaction(self, *args, **kwargs)
            finally:
                sys.stdin = _stdin
    
    

提交回复
热议问题