Reusing django auto reload function for user control commands

I like the way the django server automatically restarts itself when the code changes, so server restart is not required.

We are currently using django user management commands, which can be very time consuming.

Can we use the django server auto-restart feature for our management team?

For example, if a change to the django base code base is detected, the command reloads and resumes a very long (stateless) loop.

+4
source share
2 answers

Making the path runserver (in particular, the start method) does this using django.utils.autoreload . You will want to copy this template using your custom command.

+4
source

Whatever your control command does, abstract it from one function and call that function with 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 higher use

  autoreload.run_with_reloader(do_something, args=None, kwargs=None) 
+3
source

All Articles