Any way to use cProfile in a multi-threaded program?

M

mk

I'm stumped; I read somewhere that one would have to modify Thread.run()
method but I have never modified Python methods nor would I really
want to do it.

Is there any way to start cProfile on each thread and then combine the
stats?

Regards,
mk
 
J

Jonathan Hartley

I'm stumped; I read somewhere that one would have to modify Thread.run()
  method but I have never modified Python methods nor would I really
want to do it.

Is there any way to start cProfile on each thread and then combine the
stats?

Regards,
mk


Well, you can start cProfile from any arbitrary point within your
program, using the code:

import cProfile
import .mymodule
command = 'mymodule.myfunction()'
cProfile.runctx(command, globals(), locals(),
filename='profile.out')

Conventionally this is run at the start of your program (and myfunction
() is something that you call to startup the remainder of your
program), but one solution for you might be to run this after the
threads have been created. You'll have to run this in each thread, and
myfunction() will be the function required for the thread to do its
work. Maybe each thread should output to a different filename, using
something like filename='profile%s' % (threading.current_thread
().ident,)

As for combining the outputs from each thread, I don't know what the
cProfile output format is, so I can't help you there.

There might be better ways, but I don't know them.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top