Lightweight production-ready background task worker with cron, rate limiting and JSON observability
Project description
PyBgWorker
A lightweight, production-ready background task library for Python.
PyBgWorker provides a durable SQLite-backed task queue, scheduling (cron and countdown/ETA), rate limiting, retries, and structured observability without external infrastructure.
It is designed to be simple, reliable, and easy to deploy.
Features
- Persistent SQLite task queue
- Multi-worker safe execution
- Task scheduling: cron + countdown/ETA
- Retry + failure handling
- Task cancellation support
- Crash isolation via subprocess
- Task priority execution
- Task status tracking
- Result storage and retrieval
- Worker statistics and monitoring
- JSON structured logging
- Task duration tracking
- Rate limiting (overload protection)
- Heartbeat monitoring
- CLI tools: inspect, retry, failed, purge, cancel, stats
- Production-safe worker loop
Installation
pip install pybgworker
Basic Usage
Define a task
from pybgworker.task import task
@task(name="add")
def add(a, b):
return a + b
Enqueue a task
add.delay(1, 2)
Run worker
python -m pybgworker.cli run --app example
Cron Scheduler
Run recurring tasks:
from pybgworker.scheduler import cron
from pybgworker.task import task
@task(name="heartbeat_task")
@cron("*/1 * * * *")
def heartbeat():
print("alive")
Cron runs automatically inside the worker.
JSON Logging
All worker events are structured JSON:
{"event":"task_start","task_id":"..."}
{"event":"task_success","duration":0.12}
This enables:
- monitoring
- analytics
- alerting
- observability pipelines
Rate Limiting
Protect infrastructure from overload:
RATE_LIMIT = 5 # tasks per second
Ensures predictable execution under heavy load.
CLI Commands
Inspect queue:
python -m pybgworker.cli inspect
Retry failed task:
python -m pybgworker.cli retry <task_id>
Cancel task:
python -m pybgworker.cli cancel <task_id>
Purge queued tasks:
python -m pybgworker.cli purge
Observability
PyBgWorker logs:
- worker start
- cron events
- task start
- success
- retry
- failure
- timeout
- crash
- heartbeat errors
All machine-readable.
Design Goals
- zero external dependencies
- SQLite durability
- safe multiprocessing
- operator-friendly CLI
- production observability
- infrastructure protection
Roadmap
Planned but not yet included:
- Single-worker concurrency (process pool)
- Retry backoff + jitter policies
- Dead-letter queue for exhausted retries
- Task/result TTL and automatic DB cleanup
- Multiple named queues + routing
- Pluggable backends (Redis first)
- Cluster coordination / leader election for scheduler
- Metrics endpoint and health checks
- Dashboard API + web UI
- Workflow pipelines / DAGs
License
MIT License
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 pybgworker-0.2.2.tar.gz.
File metadata
- Download URL: pybgworker-0.2.2.tar.gz
- Upload date:
- Size: 13.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
daba045e30e750a62f888f4679f281f70cc3cad12fc2d0cd019c2a9460f30cf0
|
|
| MD5 |
1c3eaadc0baa917610cc0a8587d09cf5
|
|
| BLAKE2b-256 |
2266adaaa6ffb58c5042327eb4752145e436fae15406d863093b2510362d964d
|
File details
Details for the file pybgworker-0.2.2-py3-none-any.whl.
File metadata
- Download URL: pybgworker-0.2.2-py3-none-any.whl
- Upload date:
- Size: 16.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
14385d0a8ef40517c9fe71a372a7dbc34e81533226e0ca0df03714c6419e41d0
|
|
| MD5 |
4d73baf257e116db71e27f73b08de747
|
|
| BLAKE2b-256 |
8477ad5d021f38d68999be0d899b7313f59b4e889f22fb06eeea9d051291b7ef
|