Skip to content

Instantly share code, notes, and snippets.

@fherbine
Last active June 24, 2026 14:08
Show Gist options
  • Select an option

  • Save fherbine/f9f9edfd700959a14c78820f035cc77f to your computer and use it in GitHub Desktop.

Select an option

Save fherbine/f9f9edfd700959a14c78820f035cc77f to your computer and use it in GitHub Desktop.
Profiling
import cProfile
import threading
def _run_profiler(run):
"""Use as a decorator for Thread profiling ONLY"""
def wrapper(self):
profiler = cProfile.Profile()
profiler.enable()
run(self)
profiler.disable()
try:
return profiler.runcall(threading.Thread.run, self)
finally:
profiler.dump_stats('myprofile-%d.profile' % (self.ident,))
return wrapper
from functools import wraps
def profile(output_file="profile.prof"):
def decorator(func):
@wraps(func)
def wrapper(*args, **kwargs):
profiler = cProfile.Profile()
profiler.enable()
result = func(*args, **kwargs)
profiler.disable()
profiler.dump_stats(output_file)
return result
return wrapper
return decorator
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment