Skip to main content

A Python package for managing Django application workers using InterpreterPoolExecutor

Project description

Django Melnottam

⚠️ EXPERIMENTAL - This library is still in early development. APIs are subject to change without notice. Use at your own risk and with caution in production environments.

A Python package for managing Django application workers using InterpreterPoolExecutor from Python 3.14+. This package provides a clean abstraction for running web and task workers in isolated Python interpreters.

Overview

django-melnottam manages multiple worker processes using Python 3.14's concurrent.futures.InterpreterPoolExecutor. It provides:

  • Web Workers: ASGI/WSGI application servers using Hypercorn
  • Task Workers: Django task queue workers using django-tasks-db
  • Isolated Execution: Each worker runs in its own Python interpreter, avoiding GIL contention
  • Graceful Shutdown: Coordinated shutdown with timeout handling
  • Signal Management: Proper SIGINT handling and cleanup

Features

  • ✅ Multiple web and task workers managed in a single pool
  • ✅ Shared shutdown coordination via inter-interpreter queues
  • ✅ Configurable worker counts and binding addresses
  • ✅ Django settings module support

Requirements

  • Python 3.14+
  • Django 6.0+
  • django-tasks-db >= 0.12.0
  • hypercorn >= 0.18.0
  • rich >= 15.0.0

Installation

From GitHub

pip install git+https://github.com/yourusername/django-subinterpreter-workers.git

Usage

Command Line Interface

django-melnottam-cli <application> [options]

Arguments

  • application: Path to your WSGI application (e.g., myapp.wsgi:application)

Options

  • -w, --workers: Number of web workers (default: 2)
  • -t, --task-workers: Number of task workers (default: 1)
  • -b, --bind: Bind address for web server (default: 0.0.0.0:9001)
  • -s, --settings: Django settings module (default: from DJANGO_SETTINGS_MODULE env var)
  • -v, --verbose: Enable debug logging

Example

django-melnottam-cli myapp.wsgi:application \
  --workers 4 \
  --task-workers 2 \
  --bind 0.0.0.0:8000 \
  --settings myapp.settings.production

Programmatic Usage

from django_melnottam.manager import run_application

run_application(
    app_path="myapp.wsgi:application",
    workers=4,
    task_workers=2,
    bind="0.0.0.0:8000",
    django_settings_module="myapp.settings"
)

Using the InterpreterPoolManager Directly

from django_melnottam.manager import InterpreterPoolManager
from django_melnottam.config import WebWorkerConfig, TaskWorkerConfig

# Create manager with total worker count
pool = InterpreterPoolManager(max_workers=6)

# Configure and start web workers
web_config = WebWorkerConfig(
    worker_number=1,
    log_level=logging.INFO,
    application_path="myapp.wsgi:application",
    workers=4,
    bind="0.0.0.0:8000"
)
pool.start_worker(web_config)

# Configure and start task workers
task_config = TaskWorkerConfig(
    worker_number=2,
    log_level=logging.INFO
)
pool.start_worker(task_config)

# Gracefully shutdown
pool.shutdown(timeout=10.0)

Preferred Deployment

This library includes a built-in django-prodserver backend that provides the recommended way to deploy your Django application with web and task workers.

Production Deployment Command

To use the preferred deployment method with django-prodserver, run the following command with environment variable substitution:

BIND_ADDRESS="0.0.0.0:$PORT" WORKERS="$WORKERS" TASK_WORKERS="$INSTANCE_TASK_WORKERS" python manage.py prodserver web-with-task-worker

Configuration in Django Settings

Ensure your settings.py includes the production process configuration:

from django_melnottam.config import PositiveIntegerValue, Value

INSTALLED_APPS = [
    # ...
    "django_tasks_db",
    "django_prodserver",
    # ...
]

TASKS = {
    "default": {
        "BACKEND": "django_tasks_db.DatabaseBackend",
        "QUEUES": ["default"],
    }
}

# Production process configuration with command-line parameter support

PRODUCTION_PROCESSES = {
    "web-with-task-worker": {
        "BACKEND": "django_melnottam.prodserver_backends.subinterpreter.SubinterpreterWorkerServer",
        "ARGS": {
            "app_path": "myapp.wsgi:application",
            "django_settings_module": "myapp.settings",
            "workers": WORKERS,
            "task_workers": TASK_WORKERS,
            "bind": BIND_ADDRESS,
        },
    },
}

Environment Variables

  • PORT: The port to bind to (e.g., 8000)
  • WORKERS: Number of web workers (e.g., 4)
  • INSTANCE_TASK_WORKERS: Number of task workers (e.g., 2)

Why Use This Backend?

The SubinterpreterWorkerServer backend provides:

  • Built-in support for multiple web and task workers
  • Automatic worker management and coordination
  • Graceful shutdown handling
  • Integration with django-prodserver for seamless deployment

Configuration

Django Settings

Ensure your Django settings module is properly configured with:

# settings.py
TASKS = {
    "default": {
        "BACKEND": "django_tasks_db.InMemoryBackend",
        # or use your preferred backend
    }
}

Environment Variables

  • DJANGO_SETTINGS_MODULE: Path to your Django settings module (required if not provided via CLI)

Architecture

Worker Types

Web Workers

  • Run Hypercorn ASGI server
  • Each worker binds to the same port (handled via socket duplication)
  • Manages HTTP requests
  • Shutdown triggered by stop signal or exception

Task Workers

  • Run django-tasks-db worker
  • Process background tasks from the task queue
  • Single worker processes tasks sequentially

Communication

Workers communicate via concurrent.interpreters.Queue:

  • Shutdown Queue: Each worker monitors its shutdown queue for stop signals
  • Parent Shutdown Queue: Workers can signal the parent to shutdown
  • Worker Queue: Reserved for future inter-worker communication

Interpreter Pool

The InterpreterPoolExecutor manages a pool of Python interpreters:

  • Each submitted task runs in its own interpreter
  • Interpreters are created on-demand up to max_workers
  • No shared state between workers (strong isolation)
  • GIL is not contended between workers

Shutdown Behavior

  1. SIGINT signal received → initiates graceful shutdown
  2. Stop signal sent to all workers via shutdown queues
  3. Workers begin cleanup (close sockets, flush logs)
  4. Parent waits for worker futures with timeout
  5. InterpreterPoolExecutor shuts down
  6. Subinterpreters are cleaned up

Exceptions

The package provides custom exceptions for error handling:

  • WorkerException: Base exception
  • WorkerInitializationError: Worker failed to start
  • WorkerShutdownError: Worker shutdown failed
  • ConfigurationError: Invalid configuration
  • HookExecutionError: Hook execution failed
  • TaskExecutionError: Task execution failed
  • PoolExecutionError: Pool-level error

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_melnottam-0.1.0.tar.gz (16.0 kB view details)

Uploaded Source

Built Distribution

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

django_melnottam-0.1.0-py3-none-any.whl (13.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: django_melnottam-0.1.0.tar.gz
  • Upload date:
  • Size: 16.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.6 {"installer":{"name":"uv","version":"0.11.6","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for django_melnottam-0.1.0.tar.gz
Algorithm Hash digest
SHA256 3199754799c496bd82db9aae179d09113d176dc5b31ab388726fc962041bacc4
MD5 6e9133fcf30bc70c83c4f06cf36eb898
BLAKE2b-256 6870106643fef54f04cef2f1c1395840b1864f52a21426c56ef1b33c819d3afd

See more details on using hashes here.

File details

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

File metadata

  • Download URL: django_melnottam-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 13.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.6 {"installer":{"name":"uv","version":"0.11.6","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for django_melnottam-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fc401c7a3c1a390643bb9a4af5b2fdcc23cc882c51d4a63d88b2cbeb0b323420
MD5 1fea519578ed62183fd46e02e8177650
BLAKE2b-256 c6a0ef47647bd7f0f4ef7f3f042d54652bb524e4e60be6b61b7449049a3f87be

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