[英]Line profiling python code running as background service
我知道使用kerprof / profile / cProfile對獨立腳本進行概要分析的方式。 但是我怎樣才能將作為后台服務/長期運行的應用程序運行的python Web應用程序進行概要分析
經過深入研究並探索潛在的解決方案; 我想出了以下解決方案:
將以下函數添加到源文件中,並使用@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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.