Skip to main content

Shared aio-pika RabbitMQ client: connection, topology, publisher, consumer, router, health.

Project description

pyrmqsod

Shared aio-pika RabbitMQ client for use in FastAPI and Django: connection, topology, publisher, consumer, type-based router, health check.

Install

From git (editable):

pip install pyrmqsod'

Config

Build a RabbitMQConfig from your app settings (e.g. pydantic-settings or Django):

from pyrmqsod import RabbitMQConfig
from pyrmqsod.config import ExtraQueueConfig

config = RabbitMQConfig(
    url=settings.RABBITMQ_URL,
    in_queue=settings.RABBITMQ_IN_QUEUE,
    out_queue=settings.RABBITMQ_OUT_QUEUE,
    dlx=settings.RABBITMQ_DLX,       # optional
    dlq=settings.RABBITMQ_DLQ,        # optional
    # Declare any number of extra queues (e.g. upload/download completion queues)
    extra_queues=[
        ExtraQueueConfig(
            name=settings.RABBITMQ_UPLOAD_COMPLETED_QUEUE,
        )
    ]
    if settings.RABBITMQ_UPLOAD_COMPLETED_QUEUE
    else None,
)

Usage

  • Connection: connect_robust(config), with_channel(connection, callback), declare_topology(channel, config).
  • Publish: publish_message(channel, routing_key, payload, message_id).
  • Consumer: start_consumer(channel, config, outbox_writer, handler). outbox_writer is async (message_id, routing_key, payload) -> None; call it when publish fails to store for retry.
  • Router: make_router(registry) returns a MessageHandler that dispatches by payload["type"]; UnknownMessageTypeError for unknown types.
  • Health: check_rabbitmq_health(config) returns True/False.

Django example

# settings
RABBITMQ_URL = os.environ.get("RABBITMQ_URL", "")
RABBITMQ_IN_QUEUE = os.environ.get("RABBITMQ_IN_QUEUE", "")
RABBITMQ_OUT_QUEUE = os.environ.get("RABBITMQ_OUT_QUEUE", "")

# rabbitmq.py
from django.conf import settings
from pyrmqsod import (
    RabbitMQConfig,
    connect_robust,
    with_channel,
    declare_topology,
    start_consumer,
    make_router,
)

def get_config():
    return RabbitMQConfig(
        url=settings.RABBITMQ_URL,
        in_queue=settings.RABBITMQ_IN_QUEUE,
        out_queue=settings.RABBITMQ_OUT_QUEUE,
    )

async def run_consumer(handler):
    config = get_config()
    conn = await connect_robust(config)
    async def run(channel):
        await declare_topology(channel, config)
        async def outbox_writer(message_id, routing_key, payload):
            pass  # or your outbox implementation
        await start_consumer(channel, config, outbox_writer, handler)
    await with_channel(conn, run)

Requirements

  • Python >= 3.10
  • aio-pika >= 9.4.3

Development & tests

From the project root:

python -m venv .venv
source .venv/bin/activate  # on Windows: .venv\Scripts\activate

python -m pip install -e ".[dev]"
python -m pytest

Type checking

With dev dependencies installed (python -m pip install -e ".[dev]"), you can run ty from the project root:

ty check src/pyrmqsod tests --ignore unresolved-import

Or via pre-commit:

pre-commit run ty --all-files

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

pyrmqsod-0.1.3.tar.gz (58.1 kB view details)

Uploaded Source

Built Distribution

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

pyrmqsod-0.1.3-py3-none-any.whl (6.9 kB view details)

Uploaded Python 3

File details

Details for the file pyrmqsod-0.1.3.tar.gz.

File metadata

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

File hashes

Hashes for pyrmqsod-0.1.3.tar.gz
Algorithm Hash digest
SHA256 c9b7d45769ef893c569f755fcd9fa31056d162fcd58e8b02313fd2d9afb87d90
MD5 afd3b9db06300bf5e30c17740570f875
BLAKE2b-256 a73ca1011e7afacd28b631040748fa168d0340dd127f9595a52e9fda1076e100

See more details on using hashes here.

File details

Details for the file pyrmqsod-0.1.3-py3-none-any.whl.

File metadata

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

File hashes

Hashes for pyrmqsod-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 c305853001d2022d4afe5222435d7f69043c7bfd5997e5c8974b48027e859f1b
MD5 9d1963b39da0d2cbf8d17a199f081b6e
BLAKE2b-256 0a2328a47266ca539bed05c07d409bed4a1c245bd28cfdfc86095b61af88ebb0

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