Skip to main content

A clean, developer-friendly Python SDK wrapper for RabbitMQ (Sync & Async)

Project description

xrabbit

A production-grade, zero-boilerplate Python SDK wrapper around pika and aio-pika designed to make working with RabbitMQ clean, safe, and intuitive for both synchronous applications and asynchronous event loops.

xrabbit abstracts away low-level network loops, manual byte encoding/decoding, and messy error handling into a dead-simple, developer-friendly API.

✨ Key Features

  • Dual-Engine Architecture: Native support for standard blocking architectures (XRabbit) and high-concurrency non-blocking loops (AsyncXRabbit).
  • Zero-Boilerplate Execution: Reduce 20+ lines of standard setup to single-line operations.
  • Automatic Serialization: Pass native Python dictionaries or lists directly; the SDK handles JSON serialization and string-to-binary wire conversion transparently.
  • Resource Safety (Context Managers): Native with and async with block scopes guarantee TCP connections are cleanly reaped, entirely preventing broker connection leaks.
  • Automated Dead-Lettering (DLQ): Activate enterprise-grade error isolation with a single toggle (enable_dlq=True) to quarantine poison-pill messages automatically without crashing your workers.
  • Built-in Self-Healing: Seamlessly intercepts broken sockets or dropped brokers, triggering background retry loops instead of crashing your runtime context.

⚙️ Installation

To install xrabbit locally in editable mode for your development project:

pip install -e .

🚀 Quickstart Guide

1. Synchronous Paradigm (Classic Backend / Worker Scripts)

Throw raw Python data structures straight at your queue, and manage resource lifecycles elegantly using a standard Python context manager.

Publisher (producer.py)

from xrabbit import XRabbit, ConnectionConfig, RabbitCredentials

# Automatically handles connection initialization and teardown!
with XRabbit() as mq:
    order_payload = {
        "order_id": 9941,
        "customer": "Praveen",
        "total": 130.20
    }
    mq.publish(queue="customer_orders", message=order_payload)
    print("🚀 Message routed successfully!")

Consumer (worker.py)

from xrabbit import XRabbit

def process_order(order: dict):
    print(f"📦 Processing Order Reference: #{order['order_id']}")

with XRabbit() as mq:
    # Starts a persistent blocking listener loop
    mq.listen(queue="customer_orders", callback=process_order)

2. Asynchronous Paradigm (FastAPI / Concurrency Loops)

If you are building high-speed asynchronous APIs, use AsyncXRabbit to run entirely non-blocking messaging streams driven by asyncio.

Async Publisher

import asyncio
from xrabbit import AsyncXRabbit

async def main():
    async with AsyncXRabbit() as mq:
        await mq.publish(queue="async_orders", message={"status": "processing"})
        print("⚡ Async message dispatched!")

asyncio.run(main())

Async Consumer

import asyncio
from xrabbit import AsyncXRabbit

async def async_worker(msg: dict):
    print(f"⚡ [Async Worker]: Handling payload -> {msg}")
    await asyncio.sleep(1) # Yields control to the event loop non-blockingly

async def main():
    async with AsyncXRabbit() as mq:
        await mq.listen(queue="async_orders", callback=async_worker)

if __name__ == "__main__":
    asyncio.run(main())

🛡️ Enterprise Safety Valves

1. Dead-Letter Queue (DLQ) Protection

Prevent unhandled business logic exceptions from catching your workers in an infinite crashing retry loop. Turning on enable_dlq=True instructs the SDK to dynamically build an isolated holding pen infrastructure directly on the broker.

from xrabbit import XRabbit

def fragile_callback(msg):
    if msg.get("status") == "corrupted":
        raise ValueError("Database write failed! Quarantining packet...")

with XRabbit() as mq:
    # Automatically provisions 'email_queue.dlx' and 'email_queue.dlq' on the broker
    mq.listen(queue="email_queue", callback=fragile_callback, enable_dlq=True)

If a message crashes your callback, xrabbit intercepts the exception, rejects the payload with requeue=False, and routes it directly to the background DLQ for inspection while keeping your worker online.

2. Resilience & Auto-Healing

If your RabbitMQ container restarts or blinks during runtime execution, xrabbit suppresses ugly network stack traces and heals itself in the background:

🚨 [XRabbit Runtime Alert]: Active consumer link broken! Attempting to heal stream...
[*] XRabbit establishing connection to localhost:5672...
⚠️ [XRabbit Network Alert]: Could not reach broker. Retrying in 5 seconds...
[*] XRabbit establishing connection to localhost:5672...
[+] XRabbit successfully connected and channel opened.
[+] Re-established socket. Resuming consumption stream...

📁 Architecture Overview

xrabbit/
├── pyproject.toml           # Package metadata and dependencies
├── xrabbit/                 # Core Source Package
│   ├── __init__.py          # Master API entry points & exporters
│   ├── configs.py           # Validated configuration & data dataclasses
│   ├── client.py            # Synchronous Engine Coordinator
│   ├── async_client.py      # Asynchronousio Engine Coordinator (aio-pika)
│   ├── producer.py          # Synchronous data output layer
│   └── consumer.py          # Synchronous worker stream runtime
└── examples/                # Quickstart verification scripts

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

xrabbit-0.1.0.tar.gz (11.5 kB view details)

Uploaded Source

Built Distribution

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

xrabbit-0.1.0-py3-none-any.whl (12.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: xrabbit-0.1.0.tar.gz
  • Upload date:
  • Size: 11.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.2

File hashes

Hashes for xrabbit-0.1.0.tar.gz
Algorithm Hash digest
SHA256 b16c633bbe891240f2daedbbc360910112bf98d7d0c656e6d0e62cab5085742b
MD5 48cdcf4ee2300555be397ccc24a4dbc7
BLAKE2b-256 3aef5752d14b56e8a5a6819885421ee0b263db435bf66a07eb9284dc44c7d1ff

See more details on using hashes here.

File details

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

File metadata

  • Download URL: xrabbit-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 12.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.2

File hashes

Hashes for xrabbit-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d6ea9b3855fdf59d7bf3b5baf77c96c02b59bb9463658f8b8762a5f1fabd23ae
MD5 1202e57ae3eac2a1cd67e58cb98e96f2
BLAKE2b-256 11a31aadf5941a620bd8c35a9c017f432bfbb29aa00de68702541374e0fee432

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