Skip to main content

Task queue with SQLite sharding, rate limiting, and circuit breaker

Project description

Queue Max

Task queue library with SQLite persistence, sharding, rate limiting, and circuit breaker.

No Redis or RabbitMQ required. Zero external dependencies (except typing-extensions).

Installation

pip install queue-max

With framework integrations:

pip install queue-max[django]
pip install queue-max[fastapi]
pip install queue-max[flask]

Quick Start

from queue_max import Queue, Worker

# Create queue
queue = Queue()

# Enqueue a job
queue.enqueue({"task": "send_email", "to": "user@example.com"}, priority=2)

# Define processor
def process(payload):
    print(f"Processing: {payload}")

# Start worker
worker = Worker("worker-1", process, queue)
worker.start()

Features

SQLite Persistence -- Jobs are stored in SQLite with WAL mode. No external services needed.

Physical Sharding -- Multiple .db files allow concurrent read/write across workers.

Rate Limiting -- Token bucket algorithm shared across all workers. Configurable per minute.

Circuit Breaker -- Stops calling failing services after N consecutive errors. Recovers automatically.

Retry with Backoff -- Exponential backoff with jitter for failed jobs. Configurable max retries.

Heartbeat and Recovery -- Workers send periodic heartbeats. Orphaned jobs are recovered automatically.

Priority Queues -- Three levels: low (0), medium (1), high (2).

CLI -- Built-in command line for stats, workers, and queue management.

Configuration via Environment Variables

Variable Default Description
NUM_SHARDS 6 Number of shard databases
RATE_LIMIT_MAX 160 Max requests per minute
QUEUE_MAX_RETRIES 3 Default max retry attempts
DB_BUSY_TIMEOUT 30000 SQLite busy timeout (ms)
HEARTBEAT_INTERVAL 5000 Worker heartbeat interval (ms)
STUCK_TIMEOUT 30000 Orphan job timeout (ms)
DATA_DIR ./data Directory for shard files

API Overview

Queue

from queue_max import Queue

queue = Queue(shards=6, rate_limit=160, max_retries=3)

# Enqueue jobs
queue.enqueue(payload, pagina_id=None, priority=0, max_retries=None)
queue.enqueue_batch([{"payload": {...}}, ...])

# Process jobs
job = queue.pop_job(worker_id)
queue.complete_job(job_id, shard_id)
queue.fail_job(job_id, shard_id, error, permanent=False)

# Management
queue.retry_failed_jobs()
queue.cleanup_old_jobs(days=7)
queue.recover_orphans()
stats = queue.get_stats()

Worker

from queue_max import Worker, WorkerPool

worker = Worker("worker-1", process_function, queue)
worker.start()
worker.stop()

pool = WorkerPool([worker1, worker2])
pool.start_all()
pool.stop_all()

Decorator

from queue_max import task

@task(priority=2, max_retries=3)
def send_email(to: str, subject: str):
    return send(to, subject)

send_email.delay("user@example.com", "Hello")

CLI

queue-max stats
queue-max worker --function mymodule:myfunction --workers 4
queue-max enqueue --payload '{"task": "test"}' --priority 2
queue-max list --status failed --limit 20
queue-max retry
queue-max purge --days 7

Framework Integrations

Django

# settings.py
INSTALLED_APPS = ["queue_max.contrib.django", ...]
QUEUE_MAX = {"SHARDS": 4, "RATE_LIMIT": 160}

# tasks.py
from queue_max.contrib.django import task
@task
def my_task(user_id): ...

Management commands: python manage.py queue_worker, queue_stats, queue_purge.

FastAPI

from fastapi import FastAPI
from queue_max.contrib.fastapi import QueueMiddleware

app = FastAPI()
app.add_middleware(QueueMiddleware, max_workers=4)

Flask

from flask import Flask
from queue_max.contrib.flask import QueueExtension

app = Flask(__name__)
queue = QueueExtension(app)

@queue.task
def my_task(): ...

Performance

Cenário Throughput
Burst (20 workers, 10 shards) ~3.300 jobs/sec
Contenção (10 workers, 1 shard) ~1.660 jobs/sec
Com 30% de falhas (8 workers) Estável — circuit breaker não trip

Running Tests

pip install -e ".[dev]"
pytest tests/ -v

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

queue_max-0.1.0.tar.gz (43.5 kB view details)

Uploaded Source

Built Distribution

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

queue_max-0.1.0-py3-none-any.whl (41.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: queue_max-0.1.0.tar.gz
  • Upload date:
  • Size: 43.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for queue_max-0.1.0.tar.gz
Algorithm Hash digest
SHA256 1874cd282f4efade3c9ca5d957060455365c7e88c03172da7b33a65889713c8a
MD5 ade5da6621590d254de027dc0679c9fc
BLAKE2b-256 8e210ac2d23d078f4792c48762113d86cf470fc6eae1a966fda7f51d5ab4345c

See more details on using hashes here.

Provenance

The following attestation bundles were made for queue_max-0.1.0.tar.gz:

Publisher: publish.yml on All451/queue-max

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

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

File metadata

  • Download URL: queue_max-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 41.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for queue_max-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c19ff5719cc254a906e9fb8c734ab887ee5496e8880fcd21cbc6572d91c97c1b
MD5 88ee65c1b008cca7972649092836fabc
BLAKE2b-256 84a264d085f98f780e582d8b00b702c1fdb49ba737005c23f43b9f62ad7c1912

See more details on using hashes here.

Provenance

The following attestation bundles were made for queue_max-0.1.0-py3-none-any.whl:

Publisher: publish.yml on All451/queue-max

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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