views:

171

answers:

1

Suppose I have a python script called my_parallel_script.py that involves using multiprocessing to parallelize several things and I run it with the following command:

python -m cProfile my_parallel_script.py

This generates profiling output for the parent process only. Calls made in child processes are not recorded at all. Is it possible to profile the child processes as well?

If the only option is to modify the source, what would be the simplest way to do this?

+3  A: 

cProfile only works within a single process, so you will not automatically get the child process profiled.

I would recommend that you tweak the child process code so that you can invoke it separately as a single process. Then run it under the profiler. You probably don't need to run your system multi-process while profiling, and it will simplify the job to have only a single child running.

Ned Batchelder
I have already tried this, and unfortunately I **do** need to run the system multi-process while profiling. It seems to slow down significantly after some time, but only when it is running with multiprocessing enabled.
Fragsworth
Likely then it is resource contention, and profiling might not be the way to find it. In any case, you could try profiling all the child processes at once. You can invoke the profile programmatically in the child Python code.
Ned Batchelder