Skip to main content

Python port of queue-bus compatible with node-queue-bus and resque-bus

Project description

py-queue-bus

PyPI version Python versions

Python implementation of Queue-Bus semantics compatible with node-queue-bus and Ruby resque-bus. Uses redis-py to publish bus events and a rider worker to fan out events to subscribed queues.

Install

pip install py-queue-bus

Project on PyPI: https://pypi.org/project/py-queue-bus/

Basic usage

Publish Events (no rider needed in the publishing process):

from py_queue_bus import Bus

connection = {"host": "127.0.0.1", "port": 6379, "db": 0, "namespace": "resque"}

# Or use a Redis URL (takes precedence over host/port/db if both are provided):
# connection = {"url": "redis://:password@127.0.0.1:6379/0", "namespace": "resque"}

# Advanced: override Redis client options via `redis_kwargs` (applies even with a URL):
# connection = {
#     "url": "redis://:password@127.0.0.1:6379/0",
#     "namespace": "resque",
#     "redis_kwargs": {"socket_timeout": 5, "health_check_interval": 10},
# }

bus = Bus(connection=connection)
bus.connect()
bus.publish("order_created", {"order_id": 1, "total": 10.0})

Subscribe Events:

from py_queue_bus import Bus, Rider

connection = {"host": "127.0.0.1", "port": 6379, "db": 0, "namespace": "resque"}
# Or use a Redis URL (takes precedence over host/port/db if both are provided):
# connection = {"url": "redis://:password@127.0.0.1:6379/0", "namespace": "resque"}
app_key = "example_service"
priority = "default"
queue = f"{app_key}_{priority}"

bus = Bus(connection=connection)
bus.connect()

# Define handlers
def order_created_handler(payload):
    print("order_created_handler received:", payload)

def heartbeat_handler(payload):
    print("heartbeat_handler received:", payload)

jobs = {
    "order_created_job": order_created_handler,
    "heartbeat_job": heartbeat_handler,
}

# Subscribe
# Note: job name can differ from handler function name and event name; it must exist in the jobs dict
bus.subscribe(app_key, priority, "order_created_job", {"bus_event_type": "order_created"})
bus.subscribe(app_key, priority, "heartbeat_job", {"bus_event_type": "heartbeat_minutes"}) # See Heartbeat section below.

rider = Rider(connection=connection, jobs=jobs, queues=[queue], to_drive=True)
rider.connect()
rider.start()  # blocking worker

Scheduling (RQ)

  • publish_at / publish_in enqueue scheduled publishes using RQ. Run a worker:
    rq worker --with-scheduler queue_bus_schedule # defaults to localhost
    # if you use a Redis URL, pass it explicitly:
    export REDIS_URL=redis://:password@host:port/db
    rq worker --with-scheduler --url "$REDIS_URL" queue_bus_schedule
    
  • Use the official RQ CLI (as above) under a supervisor (systemd/docker/pm2) so it restarts if Redis drops the connection.
  • Note: Scheduling is Python-native via RQ. Node/Ruby schedulers (resque-scheduler/node-resque) won’t see Python-scheduled jobs; they only see fired jobs after RQ publishes them.

Heartbeat

  • publish_heartbeat() emits a QueueBus::Heartbeat job; riders emit heartbeat_minutes once per minute (with Redis locking) for cron-like tasks.

Examples

  • Subscriber + Rider: py-queue-bus/examples/subscriber_service.py
  • Publisher: py-queue-bus/examples/publisher_service.py
  • RQ worker stub: py-queue-bus/examples/rq_worker.py
  • Tip: In production, keep handlers in a folder and import the jobs dict into your rider entrypoint, e.g.:
    app/
      subscribers/
        __init__.py  # exports jobs = {"my_job": handler}
        order_handlers.py
      worker.py      # from subscribers import jobs; Rider(..., jobs=jobs)
    

Tests

  • Unit tests (pytest): pytest py-queue-bus/test
  • Microservice harnesses (manual cross-language checks):
    • Python service scripts: py-queue-bus/test/python_service
    • Node service scripts (npm node-queue-bus): py-queue-bus/test/node_service
    • Payloads include ts and explicit logs for debugging.

Compatibility notes

  • Redis schema matches node/resque (queues, subscription hashes/sets).
  • Payload metadata matches node/resque-bus, so Node/Ruby workers can consume events published here and vice versa.

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

py_queue_bus-0.1.2.tar.gz (15.4 kB view details)

Uploaded Source

Built Distribution

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

py_queue_bus-0.1.2-py3-none-any.whl (12.9 kB view details)

Uploaded Python 3

File details

Details for the file py_queue_bus-0.1.2.tar.gz.

File metadata

  • Download URL: py_queue_bus-0.1.2.tar.gz
  • Upload date:
  • Size: 15.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for py_queue_bus-0.1.2.tar.gz
Algorithm Hash digest
SHA256 2376eba7b910c9471932c426c3e9ed00dea70239521456f95cf77772d305d2da
MD5 6dc7461850cd4de66aef5075a7635faa
BLAKE2b-256 c622f5d4541ed759b6abee94d82b421212e5483e433e2d73cb7c2850e54b5de1

See more details on using hashes here.

File details

Details for the file py_queue_bus-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: py_queue_bus-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 12.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for py_queue_bus-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 6e0f1f51545c3c1f0bd5e78a13a5c3d93d115c75a37f227c1366cae961f50999
MD5 7a7a1f44f3475ce2192f5d10aa742e49
BLAKE2b-256 a67dff3d91a1d8269cda4d3fca2f017a9c6dbbde673008e8ce0ba6cb5648604e

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