Skip to main content

django-optimizer

Zero-code-change performance framework for Django, Celery, Pillow, DRF, and more.

pip install django-optimizer

Add to INSTALLED_APPS — that's it. No other code changes needed.

INSTALLED_APPS = [
    ...
    "django_optimizer",   # ← add this
]

What it does

django-optimizer detects which packages you have installed and automatically applies the best known optimizations for each one. Nothing is changed in your code — it works by monkey-patching at startup time.

Package Optimization
Django Persistent DB connections, atomic requests, template caching, GZip middleware
Redis hiredis parser, msgpack serializer
Pillow LANCZOS thumbnails, progressive JPEG, RGBA→RGB auto-convert, pixel limit tuning
Celery prefetch_multiplier=1, acks_late=True, worker recycling, msgpack tasks
DRF orjson renderer/parser, msgpack content negotiation
asyncio uvloop event loop policy
Python orjson as stdlib json drop-in, GC threshold tuning
HTTP Brotli compression, WhiteNoise static files
Logging QueueHandler (non-blocking), structlog JSON processors

Configuration

All optimizations are on by default. Override selectively:

# settings.py
DJANGO_OPTIMIZER = {
    # Runtime
    "USE_UVLOOP": True,
    "USE_ORJSON": True,
    "TUNE_GC": True,
    "GC_THRESHOLDS": (50_000, 500, 100),  # (gen0, gen1, gen2)

    # Database
    "OPTIMIZE_DB_CONNECTIONS": True,
    "CONN_MAX_AGE": 60,           # seconds
    "ATOMIC_REQUESTS": True,
    "DISABLE_QUERY_LOGGING": True,
    "OPTIMIZE_POSTGRESQL": True,

    # Cache
    "USE_HIREDIS": True,
    "USE_MSGPACK_CACHE": True,
    "USE_PYLIBMC": True,

    # Celery
    "OPTIMIZE_CELERY": True,
    "CELERY": {
        "MAX_TASKS_PER_CHILD": 1000,
        "BROKER_POOL_LIMIT": 10,
        "RESULT_EXPIRES": 3600,
        "USE_MSGPACK": False,      # opt-in msgpack for Celery tasks
    },

    # Pillow
    "OPTIMIZE_PILLOW": True,
    "PILLOW": {
        "MAX_IMAGE_PIXELS": 200_000_000,
        "LOAD_TRUNCATED_IMAGES": True,
        "JPEG_QUALITY": 85,
        "PROGRESSIVE_JPEG": True,
        "JPEG_BACKGROUND": (255, 255, 255),
    },

    # HTTP
    "AUTO_GZIP": True,
    "USE_WHITENOISE": True,
    "USE_BROTLI": True,

    # Templates
    "CACHE_TEMPLATES": True,
    "ADD_JINJA2_BACKEND": True,

    # Static files
    "MANIFEST_STATICFILES": True,

    # DRF
    "OPTIMIZE_DRF": True,
    "DRF_MSGPACK": True,

    # Logging
    "ASYNC_LOGGING": True,
    "USE_STRUCTLOG": True,

    # Slow query warnings
    "SLOW_QUERY_MS": 100,
}

Optional packages

Install extras for additional optimizations:

# Full optimization suite
pip install "django-optimizer[full]"

# Per-package
pip install uvloop          # async event loop
pip install orjson          # fast JSON
pip install hiredis         # fast Redis protocol parser
pip install msgpack         # binary serialization
pip install brotli          # better compression than gzip
pip install whitenoise      # static file serving
pip install structlog       # structured logging
pip install pillow-simd     # SIMD-accelerated Pillow (replaces Pillow)

Development middleware

For profiling in development:

MIDDLEWARE = [
    "django_optimizer.middleware.profiling.ProfilingMiddleware",   # adds X-Request-Time headers
    "django_optimizer.middleware.profiling.SlowQueryMiddleware",   # logs slow SQL
    ...
]

Check status

python manage.py optimizer_check

Output:

╔══════════════════════════════════════╗
║      django-optimizer  report        ║
╚══════════════════════════════════════╝

✓ Applied (8):
  • UvloopOptimizer
  • GcTuningOptimizer
  • OrjsonOptimizer
  • DatabaseConnectionOptimizer
  • HiredisOptimizer
  • PillowOptimizer
  • CeleryOptimizer
  • GZipMiddlewareOptimizer

✗ Unlockable (install packages):
  • BrotliOptimizer  →  pip install brotli
  • WhiteNoiseOptimizer  →  pip install whitenoise

💡 Recommendations:
  • Install uvloop for 2-4x faster async I/O: pip install uvloop

Writing a custom optimizer

from django_optimizer.engine import register
from django_optimizer.optimizers.base import BaseOptimizer

@register
class MyOptimizer(BaseOptimizer):
    requires = ["my_package"]       # must be importable
    settings_key = "MY_OPTIMIZER"  # DJANGO_OPTIMIZER["MY_OPTIMIZER"] = False to disable

    def apply(self):
        import my_package
        my_package.enable_fast_mode()

License

MIT

Metadata

Release files for django-optimizer 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-optimizer 0.1.0
File Size Uploaded Provenance Actions
django_optimizer-0.1.0.tar.gz 20.5 kB Without provenance Details for django_optimizer-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for django-optimizer 0.1.0
File Interpreter ABI Platform Actions
django_optimizer-0.1.0-py3-none-any.whl Python 3 none any Details for django_optimizer-0.1.0-py3-none-any.whl

Total release size: 44.8 kB

Release files / django_optimizer-0.1.0.tar.gz

Download URL django_optimizer-0.1.0.tar.gz
Size 20.5 kB
Tags Source
SHA-256 checksum
How to use checksums
c527367af1fee30b190678fbea4341659f957327589f46a73d75cbbf06715dcb
BLAKE2b-256 checksum
How to use checksums
d3630ceb31b78537536c03c914bb1a34c0c6c1c49bd6ce9dd63cfbc98ea7cda1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.9.3

Release files / django_optimizer-0.1.0-py3-none-any.whl

Download URL django_optimizer-0.1.0-py3-none-any.whl
Size 24.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
be2f9cc8a7de36f3326dff222290ea3dccfcddacc31c1a98d6a34020c7dc3775
BLAKE2b-256 checksum
How to use checksums
ccc0b2fbd4004b88817872e2cbafe187956e623c421e573cbd0aa674ccd171b6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.9.3

Security for django-optimizer 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page