-
-
Save virgiliu/614cc7cc7aa3283a60b7470038fcd9d7 to your computer and use it in GitHub Desktop.
Profiling Django Management Command
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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