0

I am aware of the way independent scripts are profiled using kerprof/profile/cProfile. But how can I profile python web application running as background service/long running application

1 Answer 1

1

After some drill down and exploring potential solutions; I came up with following solution:

  1. Add the following function into source file and decorate the original function to profile with @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
    
  2. Convert the generated /tmp/profile_bin.prof to human readable file

    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()
    

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.