Skip importing the app in the parent process when using subprocesses#2988
Closed
Kludex wants to merge 4 commits into
Closed
Skip importing the app in the parent process when using subprocesses#2988Kludex wants to merge 4 commits into
Kludex wants to merge 4 commits into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Stacked on #2987.
Summary
This is the actual fix for #2980. Since #2919,
uvicorn.run()eagerly imports the app in the parent process - but with--workersor--reloadthe parent only supervises subprocesses, which import the app themselves. The parent's copy is pure overhead. With #2987 the worker already builds its ownServerand reports readiness, so the parent no longer needs the app at all in the subprocess case.The change is two lines: only call
config.load_app()in the parent when it actually serves the app in-process.Thread workers (#2900) stay on the eager path: they aren't subprocesses, so
use_subprocessis false and the parent loads as before, primingsys.modulesfor them.Measured
200 MB app,
workers=2, Python 3.14:Workers unchanged (~247 MB each).
Behavior change
A bad import string with
--workersis no longer caught by the parent's eager load; it now surfaces when the worker fails to boot and the supervisor shuts down withSTARTUP_FAILURE(the mechanism added in #2987, covered bytest_multiprocess_stops_on_startup_failure). So #2440 fail-fast is preserved, just relocated from parent to supervisor. Under--reload, a broken app logs the error and waits for a file change instead of killing the reloader.The single-process path is untouched: it still imports eagerly before the event loop opens, keeping #941 fixed (
test_run_imports_app_before_starting_event_loop).AI Disclaimer
This PR was developed with the assistance of either Claude or Codex. I've reviewed and verified the changes.