Python:cProfile
lastmod 2019-04-12created 2019-04-105 edits
Synopsis
Simple example
It can be called from within code or from the interpreter:
You can also invoke cProfile when running a script:
Subprocesses
The things are bit more complicated if your script starts any subprocesses. In my case, I was not interested in the main script - all it does is spawning some worker subprocesses. I was interested on what’s going on inside the workers.
Let say your code is similar to this:
import multiprocessing
import time
def worker(num):
time.sleep(3)
print 'Worker:', num
if __name__ == '__main__':
for i in range(5):
p = multiprocessing.Process(target=worker, args=(i,))
p.start()
You need to add another layer of indirection:
import multiprocessing
import cProfile
import time
def worker(num):
time.sleep(3)
print 'Worker:', num
def profile_worker(num):
cProfile.runctx('worker(num)', globals(), locals(), 'profile-%d.out' %num)
if __name__ == '__main__':
for i in range(5):
p = multiprocessing.Process(target=profile_worker, args=(i,))
p.start()
And that is it. You’ll have profile-0.out to profile-4.out after the script is run.