Skip to main content

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

Project description

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_optimizer-0.1.0.tar.gz (20.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_optimizer-0.1.0-py3-none-any.whl (24.3 kB view details)

Uploaded Python 3

File details

Details for the file django_optimizer-0.1.0.tar.gz.

File metadata

  • Download URL: django_optimizer-0.1.0.tar.gz
  • Upload date:
  • Size: 20.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.3

File hashes

Hashes for django_optimizer-0.1.0.tar.gz
Algorithm Hash digest
SHA256 c527367af1fee30b190678fbea4341659f957327589f46a73d75cbbf06715dcb
MD5 eeb20bb1b4c718fb74896189981d593f
BLAKE2b-256 d3630ceb31b78537536c03c914bb1a34c0c6c1c49bd6ce9dd63cfbc98ea7cda1

See more details on using hashes here.

File details

Details for the file django_optimizer-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_optimizer-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 be2f9cc8a7de36f3326dff222290ea3dccfcddacc31c1a98d6a34020c7dc3775
MD5 d1e06addecfffac4159e5a4ff4ee03d9
BLAKE2b-256 ccc0b2fbd4004b88817872e2cbafe187956e623c421e573cbd0aa674ccd171b6

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page