Reusing django code auto-reload functionality for custom management commands
Whatever your management command is doing, abstract that to a single function and call that function using django.utils.autoreload.main
from django.utils import autoreload
def do_something(*args, **kwargs):
# management command logic
class Command(BaseCommand):
def handle(self, *args, **options):
self.stdout('This command auto reloads. No need to restart...')
autoreload.main(do_something, args=None, kwargs=None)
For django 2.2 or above use
autoreload.run_with_reloader(do_something, args=None, kwargs=None)
Checkout the way runserver (specifically the run method) does it with the django.utils.autoreload module. You'll want to copy this pattern with your custom command.