cProfile 没有方法运行调用

cProfile has no method runcall

我正在尝试使用 cProfile 分析一些 python 代码。我相信我需要使用 cProfile.runcall(),而不是 cProfile.run(),因为我想要 运行 的方法是 self.funct() 的形式,而不是简单的 funct().

当我尝试使用 cProfile.runcall、详细 here 时,出现以下错误:

AttributeError: 'module' object has no attribute 'runcall'

是否已从 cProfile 中删除 运行call 方法?如果是这样,是否有使用 cProfile.runcall(self.funct,*args) 形式的替代方法?

最小(不)工作示例:

import cProfile

def funct(a):
    print a

cProfile.runcall(funct,"Hello")

在这种情况下,问题是因为 runcall()Profile class 实例的方法,而不是模块级功能(这是您的代码尝试使用它的方式)。您需要先构造一个实例,如 documentation.

中的代码片段所示

这似乎有效(在 Python 2.7.14 中):

import cProfile

def funct(a):
    print a

pr = cProfile.Profile()
pr.enable()
pr.runcall(funct, "Hello")