我想分析我的 Python 程序以了解它为什么这么慢。我决定使用 Yappi - 因为我的程序是多线程的 - 并使用 KCacheGrind 显示结果。这是我的做法:
# Profile of the update method
def profile_update(table, start, end):
print("Profiling update() on the %s table..." % (table))
yappi.start(builtins=True)
app.update(...)
stats = yappi.get_func_stats()
output_name = "profiler_%s.out." % (table) + datetime.now().isoformat()
stats.save("profilers_outputs/" + output_name, type='callgrind')
yappi.stop()
yappi.clear_stats()
print('\n\n\n')
update方法用于从数据库中获取时间范围在start和end之间的数据。
输出文件已正确创建,但是当我启动 KCacheGrind 时,它会打印很多这样的错误:
kcachegrind(35484): Loading "profilers_outputs/profiler_benchmark.out.2019-06-26T17:21:41.147461" : 17529 : "Undefined compressed function index 586"
kcachegrind(35484): Loading "profilers_outputs/profiler_benchmark.out.2019-06-26T17:21:41.147461" : 17529 : "Invalid called function, setting to unknown"
更新此问题现已在 Yappi 中得到解决。看看这个 github pull request 和 issue。