Python:cProfile
Synopsis
Simple example
코드 내, 혹은 인터프리터에서 호출될 수 있습니다:
스크립트를 실행할 때 cProfile을 호출하는 방법도 있죠:
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.