mirror of
https://github.com/frappe/gunicorn.git
synced 2026-07-02 10:41:30 +08:00
Introduce Dirty Arbiters - a separate process pool for executing long-running, blocking operations (AI model loading, heavy computation) without blocking HTTP workers. Inspired by Erlang's dirty schedulers. Key features: - Completely separate from HTTP workers - can be killed/restarted independently - Stateful - loaded resources persist in dirty worker memory - Message-passing IPC via Unix sockets with JSON serialization - Explicit execute() API from HTTP workers - Asyncio-based for clean concurrent handling Architecture: - DirtyArbiter: manages the dirty worker pool, routes requests - DirtyWorker: executes functions, maintains state, handles requests - DirtyClient: sync/async API for HTTP workers to call dirty apps - DirtyProtocol: length-prefixed JSON messages over Unix sockets - DirtyApp: base class for dirty applications Configuration options: - dirty_apps: list of import paths for dirty applications - dirty_workers: number of dirty workers (default: 0) - dirty_timeout: task timeout in seconds (default: 300) - dirty_graceful_timeout: shutdown timeout (default: 30) Lifecycle hooks: - on_dirty_starting(arbiter) - dirty_post_fork(arbiter, worker) - dirty_worker_init(worker) - dirty_worker_exit(arbiter, worker) Includes comprehensive test suite with 164 tests covering: - Protocol encoding/decoding - Worker and arbiter lifecycle - Client sync/async APIs - Signal handling - Error handling and timeouts - Integration tests
56 lines
1.1 KiB
Python
56 lines
1.1 KiB
Python
"""
|
|
Gunicorn configuration for Dirty Workers Example
|
|
|
|
Run with:
|
|
cd examples/dirty_example
|
|
gunicorn wsgi_app:app -c gunicorn_conf.py
|
|
"""
|
|
|
|
# Basic settings
|
|
bind = "127.0.0.1:8000"
|
|
workers = 2
|
|
worker_class = "sync"
|
|
timeout = 30
|
|
|
|
# Dirty arbiter settings
|
|
dirty_apps = [
|
|
"examples.dirty_example.dirty_app:MLApp",
|
|
"examples.dirty_example.dirty_app:ComputeApp",
|
|
]
|
|
dirty_workers = 2
|
|
dirty_timeout = 300
|
|
dirty_graceful_timeout = 30
|
|
|
|
# Logging
|
|
loglevel = "info"
|
|
accesslog = "-"
|
|
errorlog = "-"
|
|
|
|
|
|
# Hooks for demonstration
|
|
def on_starting(server):
|
|
print("=== Gunicorn starting ===")
|
|
|
|
|
|
def when_ready(server):
|
|
print("=== Gunicorn ready ===")
|
|
print(f"HTTP workers: {server.num_workers}")
|
|
print(f"Dirty workers: {server.cfg.dirty_workers}")
|
|
print(f"Dirty apps: {server.cfg.dirty_apps}")
|
|
|
|
|
|
def on_dirty_starting(arbiter):
|
|
print("=== Dirty arbiter starting ===")
|
|
|
|
|
|
def dirty_post_fork(arbiter, worker):
|
|
print(f"=== Dirty worker {worker.pid} forked ===")
|
|
|
|
|
|
def dirty_worker_init(worker):
|
|
print(f"=== Dirty worker {worker.pid} initialized apps ===")
|
|
|
|
|
|
def dirty_worker_exit(arbiter, worker):
|
|
print(f"=== Dirty worker {worker.pid} exiting ===")
|