django-startup-time
Skill721 starsChanged 59 days agoMITView on GitHub
Keep heavy imports off the django.setup() path that every process (web, celery, temporal, migrate, shell, CI) pays for. Use when touching AppConfig.ready(), wiring signal receivers, editing the lazy API router (posthog/api/rest_router.py or its __init__.py shim), deferring a heavy import or profiling import time (importtime, hothog), when the startup-import-budget guard fails, or when merging master into a long-lived branch that made the router lazy.
1 other project uses a version of this.
Adopted by
Most recently taken up by
- PostHog/posthogword for word, first seen Oct 4, 2026
Other versions1 project uses a version of this.
Word for word · 1
- PostHog/posthog★ 40k100% same
Edited · 0
None found.
Wrote their own version · 0
None found.
How matches are found. Word for word: the exact same text. Edited: shares passages with this file (“99% same” means 99% of its five-word phrases also appear here; 10% or more counts). Their own version: similar meaning, or, weakest, the same name. We can’t tell which project had it first. Projects are counted among the 200 most-starred candidates, and mentions are links to the repository we found, up to 50 per site, so both counts are a floor.
In this repository119 other files it gives its agents.
- AGENTS.mdAGENTS.md
- common/AGENTS.mdAGENTS.md
- proto/AGENTS.mdAGENTS.md
- rust/AGENTS.mdAGENTS.md
- CLAUDE.mdCLAUDE.md
- common/CLAUDE.mdCLAUDE.md
- proto/CLAUDE.mdCLAUDE.md
- rust/CLAUDE.mdCLAUDE.md
- .cursor/rules/django-python.mdcCursor rule
- .cursor/rules/react-typescript.mdcCursor rule
- .cursor/rules/rust.mdcCursor rule
- adding-activity-loggingSkill
- adding-api-scopesSkill
- adding-inbound-webhooksSkill
- adding-inbox-sourcesSkill
- adding-ingestion-warningsSkill
- adding-mcp-store-serversSkill
- adding-personhog-rpcSkill
- adding-product-alertingSkill
- adding-project-secret-api-key-authSkill
- adopting-generated-api-typesSkill
- analyzing-experiment-precompute-canarySkill
- analyzing-experiment-query-performanceSkill
- analyzing-insights-across-teamsSkill
- announcing-behavior-changesSkill
- auditing-llm-gateway-paritySkill
- auditing-warehouse-source-coverageSkill
- authenticating-to-clickhouseSkill
- authoring-ci-workflowsSkill
- autoresolving-pr-conflictsSkill
- building-product-empty-statesSkill
- clickhouse-migrationsSkill
- debugging-ci-failuresSkill
- debugging-local-replaySkill
- debugging-local-task-agent-runsSkill
- debugging-mcp-analyticsSkill
- debugging-signals-pipelineSkill
- debugging-table-access-deniedSkill
- depot-ciSkill
- depot-container-buildsSkill
- depot-github-runnersSkill
- developing-subscriptionsSkill
- django-migrationsSkill
- documenting-warehouse-sourcesSkill
- editing-agents-mdSkill
- establishing-code-ownershipSkill
- extending-hobby-smoke-testsSkill
- extending-personhog-test-harnessSkill
- finding-llm-gateway-migration-candidatesSkill
- fixing-flaky-testsSkill
- gating-production-deploysSkill
- gating-sensitive-actionsSkill
- generating-clickhouse-query-performance-reportsSkill
- hogliSkill
- implementing-mcp-toolsSkill
- implementing-mcp-ui-appsSkill
- implementing-warehouse-sourcesSkill
- improving-drf-endpointsSkill
- improving-mcp-toolsSkill
- ingestion-pipeline-doctor-nodejsSkill
- The first 60 shown.
The file
Files
Django startup time
Full background — the four mechanisms (lazy router, model registration, receiver wiring, boot GC window), the regression guards, how to add code without regressing them, how to measure, and the detailed trap write-ups: docs/internal/django-startup-time.md. That doc is the single source of detail; this skill is the trigger and the checklist.
The guards live in posthog/test/repo_invariants/test_startup_import_budget.py. When one fails, defer the import — don't remove an entry to dodge it. Conversely, when you deliberately defer a significant heavy lib off setup, add it to FORBIDDEN_AT_SETUP (after confirming it's absent from a bare django.setup()). Never pin a product facade or its contracts module there: facades must stay importable from anywhere, so pin the heavy module you deferred instead. New imports nobody has named yet are caught by test_no_new_heavy_imports_at_setup: any package not in posthog/test/repo_invariants/setup_import_baseline.txt costing ≥100ms at setup fails the build — defer it; baseline only what every process genuinely needs at setup.
Defaults when adding backend code
- New app/product: register models from
models/__init__.py, wire receivers fromAppConfig.ready(), keepready()light. - New receiver: wire it at the owning
AppConfig.ready(), from a dedicated light module (activity_logging.py,signals.py) — never via the API/viewset module, even if it looks light today; those accumulate heavy imports andready()silently inherits them. - New heavy dependency (vendor SDK, Temporal/AI/ClickHouse, pandas/pyarrow/scipy): import it function-locally on the path that uses it with
# noqa: PLC0415, never at module scope. - Schema types on a setup-path module: enums from
posthog.schema_enums(cheap); pydantic models fromposthog.schemaonly inside the method that uses them. No module-levelfrom posthog.tasks...on setup paths —CeleryQueuelives inposthog.celery_queues. - New viewset/route: it no longer loads at setup — don't rely on import side effects; routes go in
rest_router.py, not the__init__.pyshim. - Where to cut: at the setup-path entry (the module
ready()wires, a model file,apps.py), never inside a module that consumes a product facade. When a facade reaches setup, the entry defers the implementation module that leads to it; facade imports stay at module scope. If the setup path needs one symbol from a heavy module, move it to a light module and re-export it. - Any deferral relocates cost — ask which process pays now, on what path, and whether that path is latency-sensitive (background workers paying lazily: fine; web workers paying on first requests: usually not).
Traps to check before you commit
One line each — the doc has the full write-up and the fix recipe for every entry.
ready()re-drags a heavy dep onto startup — importing a receiver's module runs its module-level imports too; re-measure after wiring.- Silent receiver loss — lost receivers don't error; reproduce in a process that does NOT build the router (
manage.py shell, celery,migrate). - Models vanishing from the registry — a model reachable only via a viewset import breaks
makemigrations/admin/mypy; import it frommodels/__init__.py, thendmypy stop. - Lazy-router merge conflicts — keep the
__init__.pyshim, port master's aggregator deltas intorest_router.py. - Aggregator package
__init__s — shim with PEP 562__getattr__, viaimportlib.import_moduleand an__all__whitelist (catch-alls recurse or deadlock on sibling imports). - Serializer field kwargs run at import —
choices=...inputs can't be function-locally deferred; move the constant to an import-light module. - Vanished re-exports — regenerating/shimming a module drops names other code imported from it incidentally (e.g.
schema.BaseModel); grep for consumers of the old namespace, including relative-import spellings. - Patch targets break on call-time imports —
@patch("mod.helper")stops intercepting oncemodimportshelperat call time; patch the defining module instead. - Snapshot regen on a bad merge — regenerate
.ambragainst the merged branch, then confirm it isn't masking a regression. - Measuring the wrapper, not the work — time inside the process;
importtime+tuna, not pyinstrument;grimpfor cycles and door enumeration. - Guessing where to defer — run hothog on the
importtimelog; its1-cut@column names the dominator to defer at, and--comparegives the before/after. Name this skill and hothog in the PR's agent context, so a reviewer knows how the cuts were chosen. - Phantom importtime costs from GC — re-capture with
gc.disable(); if the cost vanishes, the finding is GC, not the module. Keep the entrypoints' GC window closing in afinally.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

