---
django: 6.1.3 (f16e25dcb9fe55734ba4ea2b00ac34405b7df74a)
---

# 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` (`django/utils/autoreload.py:run_with_reloader`) and `restart_with_reloader` (`django/utils/autoreload.py:restart_with_reloader`), anchored by the sentinel environment variable `DJANGO_AUTORELOAD_ENV` (`django/utils/autoreload.py:DJANGO_AUTORELOAD_ENV`, `"RUN_MAIN"`):

```python
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`, `django/utils/autoreload.py: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` (`django/utils/autoreload.py: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!

```text figure=autoreload-two-processes
Dual-process runserver autoreloader lifecycle & verified desk/ CLI/check facts (Django 6.1.3)

1. Parent supervisor vs child worker (django/utils/autoreload.py):
   Parent (RUN_MAIN unset)  -> django.setup() [1st ready()] -> restart_with_reloader() while True loop
                            -- spawns child with RUN_MAIN='true' -->
   Child  (RUN_MAIN='true') -> django.setup() [2nd ready()] -> start_django():
                               • django-main-thread (HTTP dev server)
                               • main thread (StatReloader polling st_mtime every 1s)
                            <-- file mtime grows -> sys.exit(3) -> parent respawns! --

2. Verified CLI, checks & reloader facts on desk/:
   ManagementUtility: 31 commands (staticfiles overrides runserver; check sets checks=[])
   CheckRegistry:     30 static + 21 deploy checks; desk/ check=0, check --deploy=4 (W004, W008, W012, W016)
   StatReloader:      SLEEP_TIME=1s, watches all loaded .py modules (newsroom/views.py watched=True)
```

*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` (`django/utils/autoreload.py:get_reloader`) tries `WatchmanReloader` (`django/utils/autoreload.py:WatchmanReloader`, which subscribes to Facebook Watchman's OS file-event daemon if `pywatchman` is installed and running) and falls back to **`StatReloader`** (`django/utils/autoreload.py:StatReloader`):

- `StatReloader.tick` loops forever with `SLEEP_TIME = 1` second, calling `snapshot_files()` over every file yielded by `BaseReloader.watched_files` (`iter_all_python_module_files` (`django/utils/autoreload.py:iter_all_python_module_files`) walking `sys.modules` + `_error_files` captured from failed imports + registered `directory_globs` like template directories and `.mo` translation catalogs!).
- The moment any file's `"st_mtime"` increases, `StatReloader` fires `BaseReloader.notify_file_changed`, which sends `file_changed` (giving template caches or static handlers a chance to handle an edit without restarting; if no receiver returns `True`, `trigger_reload` calls `sys.exit(3)`!).

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

```text recording=ch13-tooling
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](test-databases.md).
