django-startup-time

Keeps heavy imports off the django.setup() path to protect startup time across all processes.

713|118|Updated Aug 11, 2020
One-click install
npx skills add https://github.com/PostHog/posthog-foss --skill django-startup-time
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: django-startup-time
Source: https://github.com/PostHog/posthog-foss/tree/main/.agents/skills/django-startup-time
Command: npx skills add https://github.com/PostHog/posthog-foss --skill django-startup-time

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Heavy imports on the django.setup() path slow down every process in a Django project — web workers, Celery, Temporal, migrations, shell, and CI. This Skill provides the checklist and guardrails to defer heavy imports, wire signal receivers correctly, and avoid regressions caught by the startup-import-budget tests.

Core Features & Use Cases

  • Import deferral guidance: Rules for keeping vendor SDKs, pandas/pyarrow/scipy, and Temporal/AI/ClickHouse imports function-local with # noqa: PLC0415 instead of at module scope.
  • Regression guard workflow: Instructions for handling failures in test_startup_import_budget.py, including when to add entries to FORBIDDEN_AT_SETUP versus the setup import baseline.
  • Trap checklist: One-line summaries of common pitfalls such as silent receiver loss, models vanishing from the registry, lazy-router merge conflicts, and phantom importtime costs from GC.
  • Use Case: When adding a new signal receiver or a heavy dependency to a Django app, use this Skill to wire it from a light module at AppConfig.ready() and confirm no new heavy import lands on the setup path.

Quick Start

Ask the assistant to review your new Django receiver or heavy dependency import against the django-startup-time checklist before committing.

Frequently Asked Questions about django-startup-time

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I defer a heavy import in Django to speed up startup?

Import the heavy dependency function-locally on the code path that uses it, marked with `# noqa: PLC0415`, instead of at module scope. After deferring, add the package to FORBIDDEN_AT_SETUP once you confirm it is absent from a bare django.setup().

How do I fix a failing startup-import-budget test in Django?

Defer the offending import off the setup path rather than removing an entry from the guard to dodge it. New packages costing 100ms or more at setup fail test_no_new_heavy_imports_at_setup unless every process genuinely needs them.

Why did my Django signal receiver silently stop firing?

Lost receivers do not raise errors, so reproduce in a process that does not build the router such as manage.py shell, celery, or migrate. Wire receivers from a dedicated light module at the owning AppConfig.ready(), never via an API or viewset module.

Why is my Django model missing from makemigrations or the admin?

A model reachable only through a viewset import vanishes from the registry because the lazy router no longer loads viewsets at setup. Import the model from the app's models/__init__.py, then run dmypy stop to clear stale state.

How do I measure Django import time accurately?

Time inside the process using importtime with tuna for visualization, grimp for cycle detection, and re-capture with gc.disable() to rule out phantom GC costs. Do not measure the wrapper or use pyinstrument for this.