Ram Rachum
Ram Rachum

Reputation: 88538

Is there a way to debug Python code which runs on a `multiprocessing.Process`?

What I would like is to be able to step-debug code that runs in a separate process using the multiprocessing package.

I remember looking for a solution about a year ago and not finding one. I was told to just do a lot of logging, but of course it is an inferior method. So maybe someone came up with a solution in the meantime? For example, some mechanism for making the newly-spawned process connect with the debugger?

Upvotes: 4

Views: 2268

Answers (3)

Marinus
Marinus

Reputation: 2210

Rather than starting your function or class via Process, just call it directly and debug as you normally would.

Upvotes: 0

evadeflow
evadeflow

Reputation: 4944

You might find WingIDE useful. Its debugger is really nice, and it even supports debugging remote processes with some minimal instrumentation to the code being debugged. It's not free, but well worth the cost, IMHO. (I'm not affiliated with Wingware in any way, just a satisfied customer...)

To enable remote debugging in Wing, you need to copy the file wingdbstub.py to the same directory as the app you want to debug, and import it at the spot in your code you want debugging to begin. (This is covered fairly thoroughly in the WingIDE docs.)

If you take this example and modify the myfunc() method to look as follows:

def myfunc(conn, commands):
    import wingdbstub
    # ... remainder same as original example

you should be able to launch WingIDE, set a breakpoint immediately after the import line, then launch the example script from a console. It should automatically connect to Wing and stop at your breakpoint.

You might find this post helpful if you have any problems getting the debug connection to work. (The WingIDE docs also do a decent job of covering connection problems.)

Upvotes: 1

Paweł Polewicz
Paweł Polewicz

Reputation: 3831

You could start the process You need to debug manually, without using the Process interface on this process.

Upvotes: 1

Related Questions