Skip to content

Instantly share code, notes, and snippets.

@virgiliu
Forked from tclancy/django_management_profile.py
Created September 23, 2018 16:59
Show Gist options
  • Save virgiliu/614cc7cc7aa3283a60b7470038fcd9d7 to your computer and use it in GitHub Desktop.
Save virgiliu/614cc7cc7aa3283a60b7470038fcd9d7 to your computer and use it in GitHub Desktop.
Profiling Django Management Command
from cProfile import Profile
from optparse import make_option
from django.core.management.base import BaseCommand
class Command(BaseCommand):
option_list = BaseCommand.option_list + (
make_option('--profile',
default=False,
help='Show cProfile information'),
)
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)
def _handle(self, *args, **options):
# actual logic lives here
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment