code: def profiled_thread(func):
      
  
      def wrapper(*args, **kwargs):
          profile = Profile()
          profile.enable()
          try:
              func(*args, **kwargs)
          finally:
              profile.disable()
          try:
              thread = current_thread()
              profile.dump_stats('profile_%s.%s.%s.log' % (getpid(), thread.name, thread.ident))
          except:
              logger.exception('Failed to dump stats')
  
      return wrapper
docs: decorator to profile a thread or function. Profiling output will be written to
      'agent_profile_<process_id>.<thread_id_>.<thread_name>.log