AppConfig's ready() function gets called multiple times on manage.py 
runserver and some tests because multiple copies of django are started, 
even though none are 'active.'

To use runserver as an example, it launches the original process's 
runserver checks if DJANGO_AUTORELOAD_ENV == "true", and if it doesn't 
exist, it spawns child processes with that ENV var set.

At the same codepoint in the child, since DJANGO_AUTORELOAD_ENV is set, it 
issues start_django (with the reloader argument set).

In other words, you can tell when you're in a running 'server' and not a 
doomed launcher by checking if DJANGO_AUTORELOAD_ENV == "true", but only 
when using runserver. If it's unset, we could either be a non-runserver 
django, or a doomed launcher.

If DJANGO_AUTORELOAD_ENV was set to "false" in run_with_reloader when we 
spawn the child process, then in AppConfig we can guard the ready() call, 
since a value of "true" or None means we're an active running server. We 
can use

if os.environ.get(DJANGO_AUTORELOAD_ENV) != "false":

and only trigger ready() once, which I think would be the 'expected' 
behavior the average user would expect.

Alternately, the same thing could be done, but instead of guarding the 
ready() call, a function (is_autoloader, maybe?) could be exposed to the 
end user, to let them make the decision themselves.

To my knowledge, as things are now, there's no other way to determine this. 
The best you can do is check for a flag on AppConfig that you setattr if it 
wasn't, but there's no way to directly tell which process you're in.

Thoughts?

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers  (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-developers+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-developers/36d6ba80-34a5-4aed-aca3-4a30234c3f92n%40googlegroups.com.
  • Pro... Steffan Pease
    • ... 'Adam Johnson' via Django developers (Contributions to Django itself)
      • ... Steffan Pease

Reply via email to