Proposal: Clarify the ENV var that controls the autoreloader so AppConfig's ready() can be have a guard.

79 views
Skip to first unread message

Steffan Pease

unread,
Oct 15, 2022, 5:03:32 PM10/15/22
to Django developers (Contributions to Django itself)
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?

Adam Johnson

unread,
Oct 18, 2022, 2:56:28 PM10/18/22
to django-d...@googlegroups.com
Steffan

Your question suffers from the ”X/Y problem”. You’re asking whether to change Django to make Y easy, without explaining the X you’re trying to achieve. *Why* do you want to know if you're in the runserver main process?

Also, it may work to check the running command from sys.argv ( https://adamj.eu/tech/2020/05/14/how-to-check-the-running-django-command/ ). If it’s runserver, and the env var isn’t set, then you can tell.

Thanks,

Adam

--
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-develop...@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.

Steffan Pease

unread,
Oct 18, 2022, 8:03:36 PM10/18/22
to Django developers (Contributions to Django itself)
Hi Adam,

This isn't so much a tech support question- I don't have a specific personal issue I'm trying to solve with this suggestion, it's more that I feel like this part of django works in a counter-intuitive way (best exemplified that the Django documentation includes various warnings about it not working as expected) and it's a problem that is, for some measure of 'easy', easily solved.

Thanks!
Steffan
Reply all
Reply to author
Forward
0 new messages