我正在尝试分析实例方法,所以我做了类似的事情:

import cProfile

class Test():

    def __init__(self):
        pass

    def method(self):
        cProfile.runctx("self.method_actual()", globals(), locals())

    def method_actual(self):
        print "Run"

if __name__ == "__main__":
    Test().method()

但是现在,当我希望“方法”返回由“method_actual”计算出的值时,就会出现问题。我真的不想两次调用“method_actual”。

还有另一种方法可以使线程安全吗? (在我的应用程序中,cProfile数据保存到以args之一命名的数据文件中,因此它们不会被破坏,以后我可以将它们组合起来。)

最佳答案

我发现您可以执行以下操作:

prof = cProfile.Profile()
retval = prof.runcall(self.method_actual, *args, **kwargs)
prof.dump_stats(datafn)

缺点是它没有记录。

关于python - 使用cProfile时返回值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/1584425/

10-11 09:13