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 On Sat, Oct 15, 2022 at 4:09 AM Steffan Pease <steffanpe...@gmail.com> wrote: > 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 > <https://groups.google.com/d/msgid/django-developers/36d6ba80-34a5-4aed-aca3-4a30234c3f92n%40googlegroups.com?utm_medium=email&utm_source=footer> > . > -- 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/CAMyDDM0EXooGULj_kza0iRjkpwqx%2BoG_i26NGBMxsdAoaj_Fjg%40mail.gmail.com.