代码之家  ›  专栏  ›  技术社区  ›  Herman Schaaf

关于python分析的问题

  •  2
  • Herman Schaaf  · 技术社区  · 15 年前

    我试图用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')
    

    如果这看起来很奇怪,那是因为我已经尽一切努力消除了错误“名称doProfile not defined”。即使现在,runProfiler方法被调用,“runProfiler invoked”被打印出来,但随后我得到了刚才描述的错误。我做错什么了?

    2 回复  |  直到 15 年前
        1
  •  1
  •   AndiDog    15 年前

    在这种情况下,您应该将必要的上下文传递给 cProfile.runctx

    cProfile.runctx("doProfile()", globals(), locals(), "profile.log")
    
        2
  •  0
  •   jchl    15 年前

    另一种方法是使用 runcall Profile 反对。

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