Django 6.1.3

13.3 Commands, checks, and testing

Why runserver runs two processes

Why does manage.py runserver 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.ready(), runs "python manage.py runserver", 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:

  1. Parent supervisor ("RUN_MAIN" unset): ManagementUtility.execute runs django.setup() (firing AppConfig.ready the first time — unless a SyntaxError hits, which django.utils.autoreload.check_errors catches so the supervisor stays alive!). Then "runserver" calls run_with_reloader, which sees "RUN_MAIN != 'true'" and enters restart_with_reloader: an infinite "while True:" loop calling subprocess.run(args, env={**os.environ, "RUN_MAIN": "true"}). Whenever the child exits with magic exit code 3 (trigger_reload, trigger_reload), the parent loops and spawns a brand-new child; any other exit code (0 on Ctrl+C, 1 on fatal config error) exits the parent too!
  2. Child worker ("RUN_MAIN == 'true'"): The child boots Python from scratch, runs django.setup() a second time (firing AppConfig.ready again!), and enters start_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.run(django_main_thread)") so OS signals (signal.SIGINT / signal.SIGTERM) work reliably!
Figure 13.2 Every command count, check ID, and reloader constant is verified directly against 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:

Watch DJANGO_AUTORELOAD_ENV, StatReloader, and iter_all_python_module_files verifying "newsroom/views.py" is watched live on "desk/":

Recorded on Django 6.1.3 ch13-tooling script
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.