Skip to main content

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pybgworker-0.2.2.tar.gz (13.8 kB view details)

Uploaded Source

Built Distribution

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

pybgworker-0.2.2-py3-none-any.whl (16.1 kB view details)

Uploaded Python 3

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

Hashes for pybgworker-0.2.2.tar.gz
Algorithm Hash digest
SHA256 daba045e30e750a62f888f4679f281f70cc3cad12fc2d0cd019c2a9460f30cf0
MD5 1c3eaadc0baa917610cc0a8587d09cf5
BLAKE2b-256 2266adaaa6ffb58c5042327eb4752145e436fae15406d863093b2510362d964d

See more details on using hashes here.

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

Hashes for pybgworker-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 14385d0a8ef40517c9fe71a372a7dbc34e81533226e0ca0df03714c6419e41d0
MD5 4d73baf257e116db71e27f73b08de747
BLAKE2b-256 8477ad5d021f38d68999be0d899b7313f59b4e889f22fb06eeea9d051291b7ef

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