作为后台服务运行的python代码行分析
我知道使用kerprof/profile/cProfile分析独立脚本的方式。但是,在深入研究和探索潜在的解决方案之后,我如何分析作为后台服务/长期运行的应用程序运行的python web应用程序;我提出了以下解决方案:作为后台服务运行的python代码行分析,python,profiling,Python,Profiling,我知道使用kerprof/profile/cProfile分析独立脚本的方式。但是,在深入研究和探索潜在的解决方案之后,我如何分析作为后台服务/长期运行的应用程序运行的python web应用程序;我提出了以下解决方案: 将以下函数添加到源文件中,并使用@do_cprofile将原始函数修饰为profile import cProfile def do_cprofile(func): def profiled_func(*args, **kwargs): profile
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()