Skip to content

Instantly share code, notes, and snippets.

@tclancy
Last active January 3, 2023 22:18
Show Gist options
  • Save tclancy/4236077 to your computer and use it in GitHub Desktop.
Save tclancy/4236077 to your computer and use it in GitHub Desktop.
Profiling Django Management Command
from cProfile import Profile
from django.core.management.base import BaseCommand
class ProfileEnabledBaseCommand(BaseCommand):
"""Enable profiling a command with --profile.
Requires child class to define _handle instead of handle.
via https://gist.github.com/dfrankow
"""
def add_arguments(self, parser):
parser.add_argument('--profile', action='store_true', default=False)
def handle(self, *args, **options):
if options.get('profile', False):
profiler = Profile()
profiler.runcall(self._handle, *args, **options)
profiler.print_stats()
else:
self._handle(*args, **options)
@dfrankow
Copy link

:)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment