A simple RabbitMQ client for Python
Project description
MicroRabbit
MicroRabbit is a lightweight, asynchronous Python framework for working with RabbitMQ. It simplifies the process of setting up RabbitMQ consumers and publishers, making it easy to build microservices and distributed systems.
Features
- Asynchronous message handling using
asyncio - Simple decorator-based message routing
- Plugin system for modular code organization
- Easy-to-use client configuration
- Built-in logging support
Installation
pip install microrabbit
Quick Start
Here's a simple example of how to use MicroRabbit:
import asyncio
import logging
from microrabbit import Client
from microrabbit.types import QueueOptions, ConsumerOptions
client = Client(
host="amqp://guest:guest@localhost/",
plugins="./plugins"
)
log = logging.getLogger(__file__)
logging.basicConfig(level=logging.INFO)
@Client.on_message("queue_name")
async def test(data: dict) -> dict:
log.info(f"Received message {data}")
return {"connected": True}
@Client.on_message("queue_name2", QueueOptions(exclusive=True), ConsumerOptions(no_ack=True))
async def test2(data: dict) -> dict:
log.info(f"Received message {data}")
return {"connected": True}
@client.on_ready
async def on_ready():
log.info("[*] Waiting for messages. To exit press CTRL+C")
result = await client.simple_publish("queue_name2", {"test": "data"}, timeout=2, decode=True)
log.info(result)
if __name__ == "__main__":
asyncio.run(client.run())
Usage
Client Configuration
Create a Client instance with the following parameters:
host: RabbitMQ server URLplugins: Path to the plugins folder (optional)
from microrabbit import Client
client = Client(
host="amqp://guest:guest@localhost/",
plugins="./plugins"
)
Message Handling
Use the @Client.on_message decorator to define a message handler. The decorator takes the queue name as an argument.
from microrabbit import Client
@Client.on_message("queue_name")
async def handler(data: dict):
# Process the message
return response_data # Serializeable data
Ready Event
Use the @client.on_ready decorator to define a function that runs when the client is ready:
from microrabbit import Client
client = Client(
host="amqp://guest:guest@localhost/",
plugins="./plugins"
)
@client.on_ready
async def on_ready():
print("Client is ready")
Running the Client
Run the client using asyncio.run(client.run()):
import asyncio
from microrabbit import Client
client = Client(
host="amqp://guest:guest@localhost/",
plugins="./plugins"
)
if __name__ == "__main__":
asyncio.run(client.run())
Publishing Messages
Use the simple_publish method to publish a message to a queue:
result = await client.simple_publish("queue_name", {"test": "data"}, timeout=2, decode=True)
Running with context manager
import asyncio
from microrabbit import Client
client = Client(
host="amqp://guest:guest@localhost/",
plugins="./plugins"
)
async def main():
async with client:
await client.run()
if __name__ == "__main__":
asyncio.run(main())
Plugins
MicroRabbit supports a plugin system. Place your plugin files in the specified plugins folder, and they will be automatically loaded by the client.
Plugin Example
# ./plugins/test_plugin.py
from microrabbit import Client
@Client.on_message("test_queue")
async def test_handler(data: dict):
print(f"Received message: {data}")
return {"status": "ok"}
Advanced Usage
Queue Options
Use the QueueOptions class to specify queue options:
from microrabbit.types import QueueOptions
@Client.on_message("queue_name", QueueOptions(exclusive=True))
async def handler(data: dict):
# Process the message
return response_data
Consumer Options
Use the ConsumerOptions class to specify consumer options:
from microrabbit.types import ConsumerOptions
@Client.on_message("queue_name", ConsumerOptions(no_ack=True))
async def handler(data: dict):
# Process the message
return response_data
Contributing
Contributions are welcome! For feature requests, bug reports, or questions, please open an issue. If you would like to contribute code, please submit a pull request.
License
This project is licensed under the MIT License - see the LICENSE file for details.
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file microrabbit-0.3.3.1.tar.gz.
File metadata
- Download URL: microrabbit-0.3.3.1.tar.gz
- Upload date:
- Size: 8.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
020e9443fc0e3d174a7153eea86a5c2a5de01c84b189f512ed1cfab1cc3e1fb5
|
|
| MD5 |
2f874a17ef2d15269167f2f90062a908
|
|
| BLAKE2b-256 |
e14ec956b12b9ed02f6edf5f5d0c3264fa097e55effdd44a4622560ee13c4e60
|
File details
Details for the file microrabbit-0.3.3.1-py3-none-any.whl.
File metadata
- Download URL: microrabbit-0.3.3.1-py3-none-any.whl
- Upload date:
- Size: 9.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
12c9e96d79aab49c439014a18cfd03f5c1930688ef36e45828bddbdd80690ed7
|
|
| MD5 |
dbb3351c4e4a6d3cf5031be138409f30
|
|
| BLAKE2b-256 |
b2cb8339650645d388e8dde82d7877db9eed6924ac5918c93dc0740018bc94db
|