0
我知道使用kerprof/profile/cProfile对独立脚本进行概要分析的方式。但我如何能够配置python web应用程序运行作为后台服务/长时间运行的应用程序作为后台服务运行的行剖析python代码
我知道使用kerprof/profile/cProfile对独立脚本进行概要分析的方式。但我如何能够配置python web应用程序运行作为后台服务/长时间运行的应用程序作为后台服务运行的行剖析python代码
经过深入研究和探索潜在的解决方案;我想出了以下解决方案:
以下函数添加到源文件和装饰原有的功能与@do_cprofile
import cProfile
def do_cprofile(func):
def profiled_func(*args, **kwargs):
profile = cProfile.Profile()
try:
profile.enable()
result = func(*args, **kwargs)
profile.disable()
return result
finally:
profile.dump_stats('/tmp/profile_bin.prof')
return profiled_func
到配置文件转换生成的/tmp/profile_bin.prof
人类可读的文件
import pstats
f = open('/tmp/human_readable_profile.prof', 'w')
stats = pstats.Stats('/tmp/profile_bin.prof', stream=f)
stats.sort_stats('cumulative').print_stats()
f.close()