Why is run called twice in the Django dev server?

前端 未结 2 1570
感情败类
感情败类 2020-12-09 10:20

I want to make the Django development server do something before it starts running. To do this, I created a new app, added it to the top of INSTALLED_APPS, and

相关标签:
2条回答
  • 2020-12-09 10:58

    The auto-reloader process turned out to be the culprit; turns out the autoreload process gets the same arguments, and goes through the same initialization process, as the original. The solution was to have the pre-server code execute only if it's not running in the process spawned by the autoreloader, which can be detected through an environment variable:

    import os
    from django.contrib.staticfiles.management.commands.runserver import Command as RunserverCommand
    class Command(RunserverCommand):
        def run(self, *args, **options):
            if os.environ.get('RUN_MAIN') != 'true':
                self.stdout.write('About to start running on ' + self.addr)
            super(Command, self).run(*args, **options)
    
    0 讨论(0)
  • 2020-12-09 11:01

    The local development server runs a separate process for the auto-reloader. You can turn off the auto-reload process by passing the --noreload flag.

    python manage.py runserver --noreload
    
    0 讨论(0)
提交回复
热议问题