Fast Rabbit is an advanced, asynchronous RabbitMQ client for Python, designed to simplify the integration and management of RabbitMQ interactions. It provides a robust and efficient way to publish and consume messages asynchronously, leveraging the power of asyncio
and aio_pika
.
To install Fast Rabbit, run the following command in your terminal:
pip install fast_rabbit
Ensure you have Python 3.7 or newer, as Fast Rabbit leverages the latest features of asyncio
.
First, initialize the FastRabbitEngine
with your RabbitMQ server's AMQP URL:
from fast_rabbit import FastRabbitEngine
engine = FastRabbitEngine(amqp_url="amqp://user:password@localhost/")
To publish messages to a queue, use the publish
method:
await engine.publish("queue_name", "Hello, RabbitMQ!")
To consume messages, you first define a handler function and then register it for a specific queue:
async def message_handler(body: str):
print(f"Received message: {body}")
engine.subscribe("queue_name")(message_handler)
await engine.run()
FastRabbitRouter
allows you to organize your message handlers similarly to routing in web frameworks like FastAPI. This is particularly useful for larger applications with multiple message types and queues.
Create a FastRabbitRouter
instance and use the route
decorator to register message handlers:
from fast_rabbit import FastRabbitRouter
router = FastRabbitRouter()
@router.route("queue_name")
async def handle_message(body: str):
print(f"Received message: {body}")
Once your routes are defined, include them in the FastRabbitEngine
instance:
engine.include_subscriptions(router)
Start the engine to begin consuming messages:
await engine.run()
Fast Rabbit is designed to robustly handle connection and channel errors. However, you should implement your own error handling within message handlers to manage application-specific errors gracefully.
Contributions to Fast Rabbit are welcome! Please refer to the project's issues page to report bugs or suggest features. For pull requests, kindly follow the project's coding standards and include tests for new functionality.
Fast Rabbit is released under the MIT License. See the LICENSE file in the project repository for more details.