2010-10-20 57 views
2

我試圖在python中對我的應用程序進行分析。我正在使用cProfile庫。我需要剖析我的應用程序的onFrame函數,但這是由外部應用程序調用的。我試過的東西負荷,但此刻我已在我的onFrame方法如下:關於python分析的問題

runProfiler(self) 

,然後我的課堂外,我有以下:

o = None 

def doProfile(): 
    print "doProfile invoked" 
    o.structure.updateOrders 

def runProfiler(self): 
    print "runProfiler invoked" 
    o = self 
    cProfile.run('doProfile()', 'profile.log') 

如果這似乎有些奇怪,這是因爲我已經嘗試了一切,以擺脫「名稱doProfile未定義」的錯誤。即使現在,runProfiler方法被調用,並且「runProfiler調用」被打印,但是然後我得到剛剛描述的錯誤。我究竟做錯了什麼?

回答

1

在這種情況下,應該通過必要的上下文cProfile.runctx

cProfile.runctx("doProfile()", globals(), locals(), "profile.log") 
0

另一種方法是使用一個Profile對象的runcall方法。

profiler = cProfile.Profile() 
profiler.runcall(doProfile) 
profiler.dump_stats("profile.log")