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.0hypercorn >= 0.18.0rich >= 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: fromDJANGO_SETTINGS_MODULEenv 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-prodserverfor 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-dbworker - 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
- SIGINT signal received → initiates graceful shutdown
- Stop signal sent to all workers via shutdown queues
- Workers begin cleanup (close sockets, flush logs)
- Parent waits for worker futures with timeout
- InterpreterPoolExecutor shuts down
- Subinterpreters are cleaned up
Exceptions
The package provides custom exceptions for error handling:
WorkerException: Base exceptionWorkerInitializationError: Worker failed to startWorkerShutdownError: Worker shutdown failedConfigurationError: Invalid configurationHookExecutionError: Hook execution failedTaskExecutionError: Task execution failedPoolExecutionError: Pool-level error
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3199754799c496bd82db9aae179d09113d176dc5b31ab388726fc962041bacc4
|
|
| MD5 |
6e9133fcf30bc70c83c4f06cf36eb898
|
|
| BLAKE2b-256 |
6870106643fef54f04cef2f1c1395840b1864f52a21426c56ef1b33c819d3afd
|
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fc401c7a3c1a390643bb9a4af5b2fdcc23cc882c51d4a63d88b2cbeb0b323420
|
|
| MD5 |
1fea519578ed62183fd46e02e8177650
|
|
| BLAKE2b-256 |
c6a0ef47647bd7f0f4ef7f3f042d54652bb524e4e60be6b61b7449049a3f87be
|