13.3 Commands, checks, and testing
Why runserver runs two processes
Why does manage spawn a parent supervisor and a child worker, why does AppConfig.ready run twice in development, and how does StatReloader decide when to restart?
Almost every Django developer eventually puts a print() or background scheduler startup inside AppConfig, runs "python manage, and watches it execute twice! The reason is neither a bug nor a race condition: Python cannot safely reload modified modules in place inside a running interpreter (existing class references, metaclass registries, and signal connections would point at stale objects!), so Django’s autoreloader restarts an entire fresh child Python process every time you save a file.
The parent/child reload protocol and exit code 3
Open run_with_reloader and restart_with_reloader, anchored by the sentinel environment variable DJANGO_AUTORELOAD_ENV (DJANGO_AUTORELOAD_ENV, "RUN_MAIN"):
if os.environ.get(DJANGO_AUTORELOAD_ENV) == "true":
reloader = get_reloader()
start_django(reloader, main_func, *args, **kwargs)
else:
exit_code = restart_with_reloader()
sys.exit(exit_code)
Trace both processes from the moment you hit Enter:
- Parent supervisor (
"RUN_MAIN"unset):ManagementUtilityruns.execute django.setup()(firingAppConfig.readythe first time — unless aSyntaxErrorhits, whichdjangocatches so the supervisor stays alive!). Then.utils .autoreload .check_errors "runserver"callsrun_with_reloader, which sees"RUN_MAIN != 'true'"and entersrestart_with_reloader: an infinite"while True:"loop callingsubprocess. Whenever the child exits with magic exit code.run(args, env={**os .environ, "RUN_MAIN": "true"}) 3(trigger_reload,trigger_reload), the parent loops and spawns a brand-new child; any other exit code (0on Ctrl+C,1on fatal config error) exits the parent too! - Child worker (
"RUN_MAIN == 'true'"): The child boots Python from scratch, runsdjango.setup()a second time (firingAppConfig.readyagain!), and entersstart_django:- Spawns a daemon thread named
"django-main-thread"running the actual WSGI dev server (inner_run, system checks, migration warnings, and HTTP socket loop). - Keeps the child’s main thread running
BaseReloader.run("reloader) so OS signals (.run(django_main_thread)" signal.SIGINT/signal.SIGTERM) work reliably!
- Spawns a daemon thread named
ch13-tooling. Passing "--noreload" skips run_with_reloader completely (running AppConfig.ready once in a single process), while checking "os.environ.get('RUN_MAIN') == 'true'" isolates dev-only startup hooks to the worker process.How StatReloader vs. WatchmanReloader detects edits
get_reloader tries WatchmanReloader (WatchmanReloader, which subscribes to Facebook Watchman’s OS file-event daemon if pywatchman is installed and running) and falls back to StatReloader:
StatReloaderloops forever with.tick SLEEP_TIME = 1second, callingsnapshot_files()over every file yielded byBaseReloader(.watched_files iter_all_python_module_fileswalkingsys.modules+_error_filescaptured from failed imports + registereddirectory_globslike template directories and.motranslation catalogs!).- The moment any file’s
"st_mtime"increases,StatReloaderfiresBaseReloader, which sends.notify_file_changed file_changed(giving template caches or static handlers a chance to handle an edit without restarting; if no receiver returnsTrue,trigger_reloadcallssys.exit(3)!).
Watch DJANGO_AUTORELOAD_ENV, StatReloader, and iter_all_python_module_files verifying "newsroom/ is watched live on "desk/":
3. Autoreload RUN_MAIN protocol and StatReloader
autoreload_env='RUN_MAIN' reloader=StatReloader sleep_time_s=1 reload_exit_code=3 watches_newsroom_views=True
That wraps up the interactive terminal commands (manage.py, checks, and "runserver"). Next we open Django’s testing architecture — starting with how DiscoverRunner creates, serializes, clones, and destroys isolated test databases in Creating, cloning, and tearing down test databases.