我正在尝试用 Python 分析我的应用程序。我正在使用 cProfile 库。我需要分析我的应用程序的 onFrame 函数,但这是由外部应用程序调用的。我已经尝试了很多东西,但目前我的 onFrame 方法中有以下内容:
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')
如果这看起来很奇怪,那是因为我已经尝试了所有方法来消除错误“name doProfile not defined”。即使是现在,runProfiler 方法也会被调用,并且会打印“runProfiler invoked”,但随后我会收到刚才描述的错误。我做错了什么?
请您参考如下方法:
在这种情况下,您应该将必要的上下文传递给 cProfile.runctx :
cProfile.runctx("doProfile()", globals(), locals(), "profile.log")
