Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to attach debugger to a python subproccess?

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 already running processes.

Are there any smarter python debuggers which can be attached to a subprocess?

like image 457
grep Avatar asked Jan 17 '11 18:01

grep


People also ask

How do you attach a debugger to a Process?

After the process is running, select Debug > Attach to Process or press Ctrl+Alt+p in Visual Studio, and use the Attach to Process dialog to attach the debugger to the process.

What is attach to Process in PyCharm?

Attach to process Last modified: 09 August 2022. Run | Attach to Process Ctrl+Alt+F5. PyCharm provides a way to attach the debugger to to a Python local process, while running a Python script launched either from your operating system or using the PyCharm terminal, but NOT in the debug mode.

How do I enable debug mode in PyCharm?

Just right-click any line in the editor and select the Debug <filename> command from the context menu. After the program has been suspended, use the debugger to get the information about the state of the program and how it changes during running.


2 Answers

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() 
like image 199
Romuald Brunet Avatar answered Oct 12 '22 22:10

Romuald Brunet


Winpdb is pretty much the definition of a smarter Python debugger. It explicitly supports going down a fork, not sure it works nicely with multiprocessing.Process() but it's worth a try.

For a list of candidates to check for support of your use case, see the list of Python Debuggers in the wiki.

like image 29
TryPyPy Avatar answered Oct 13 '22 00:10

TryPyPy