Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
作为后台服务运行的python代码行分析_Python_Profiling - Fatal编程技术网

作为后台服务运行的python代码行分析

作为后台服务运行的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

我知道使用kerprof/profile/cProfile分析独立脚本的方式。但是,在深入研究和探索潜在的解决方案之后,我如何分析作为后台服务/长期运行的应用程序运行的python web应用程序;我提出了以下解决方案:

  • 将以下函数添加到源文件中,并使用@do_cprofile将原始函数修饰为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()