問題描述
我正在努力弄清楚如何分析一個簡單的多進程 python 腳本
I'm struggling to figure out how to profile a simple multiprocess python script
import multiprocessing
import cProfile
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,))
cProfile.run('p.start()', 'prof%d.prof' %i)
我正在啟動 5 個進程,因此 cProfile 會生成 5 個不同的文件.在每個內(nèi)部,我想看到我的方法worker"運行大約需要 3 秒,但我只看到start"方法內(nèi)部發(fā)生的事情.
I'm starting 5 processes and therefore cProfile generates 5 different files. Inside of each I want to see that my method 'worker' takes approximately 3 seconds to run but instead I'm seeing only what's going on inside the 'start'method.
如果有人可以向我解釋這一點,我將不勝感激.
I would greatly appreciate if somebody could explain this to me.
import multiprocessing
import cProfile
import time
def test(num):
time.sleep(3)
print 'Worker:', num
def worker(num):
cProfile.runctx('test(num)', globals(), locals(), 'prof%d.prof' %num)
if __name__ == '__main__':
for i in range(5):
p = multiprocessing.Process(target=worker, args=(i,))
p.start()
推薦答案
你正在分析進程啟動,這就是為什么你只看到 p.start()
中發(fā)生了什么比如說,一旦子進程啟動,p.start()
就會返回.您需要在 worker
方法中進行概要分析,該方法將在子進程中被調(diào)用.
You're profiling the process startup, which is why you're only seeing what happens in p.start()
as you say—and p.start()
returns once the subprocess is kicked off. You need to profile inside the worker
method, which will get called in the subprocesses.
這篇關(guān)于Python 多進程分析的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!