Easily send and receive messages to and from RabbitMQ message broker
Project description
RabbitMQ Decorator
- Work in progress
Easily send and receive messages to and from RabbitMQ message broker.
Installation
pip install rabbitmq-decorator
Usage
Optional:
export RABBITMQ_HOST=localhost
export RABBITMQ_PORT=5672
export RABBITMQ_USERNAME=username
export RABBITMQ_PASSWORD=password
Example:
import asyncio
from typing import Dict, Any
from rabbitmq_decorator import RabbitMQHandler, RabbitMQConsumer, RabbitMQProducer, RabbitMQMessage, Exchange
event_loop = asyncio.get_event_loop()
# If environment variables was set
rabbit_handler = RabbitMQHandler(event_loop=event_loop)
# Or if environment variables wasn't set
rabbit_handler = RabbitMQHandler(
event_loop=event_loop,
rabbitmq_host="localhost",
rabbitmq_port=5672,
rabbitmq_username="username",
rabbitmq_password="password"
)
@rabbit_handler
class SomeClass:
EXCHANGE = Exchange("test_exchange", durable=True)
_producer: RabbitMQProducer = rabbit_handler.get_new_producer(EXCHANGE)
def some_unrelated_function(self):
pass
@RabbitMQConsumer(EXCHANGE)
async def consumer_1(self, body: Dict[str, Any]):
print("I'm getting all the messages on test_exchange")
self._producer.publish(RabbitMQMessage(routing_key="response.consumer1", body={"status": "Success"}))
@RabbitMQConsumer(EXCHANGE, route="*.*.*")
async def consumer_2(self, body: Dict[str, Any]):
print("I'm getting only the messages on test_exchange with *.*.* as routing-key ")
response_exchange = Exchange("response_exchanges", durable=True)
self._producer.publish(
exchange=response_exchange,
message=RabbitMQMessage(routing_key="response.consumer2", body={"status": "Success"})
)
# ================ main ================
async def main():
"""do something"""
if __name__ == '__main__':
some_class = SomeClass()
rabbit_handler.start(some_class)
event_loop.run_until_complete(main())
rabbit_handler.stop_consume(some_class)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
rabbitmq-decorator-0.0.1.tar.gz
(11.4 kB
view details)
Built Distribution
File details
Details for the file rabbitmq-decorator-0.0.1.tar.gz
.
File metadata
- Download URL: rabbitmq-decorator-0.0.1.tar.gz
- Upload date:
- Size: 11.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.9.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ac1acfc1484e27366b3d79c31b674caaefda66e209eb4d26c43b10795eb7fda3 |
|
MD5 | fe82a28907a0a99b429e847e847f1a9b |
|
BLAKE2b-256 | 3f09871e0d5b1c8eb792b2a70b6c70dae3eb3670dd0f3e7293dd02f472a6479f |
File details
Details for the file rabbitmq_decorator-0.0.1-py3-none-any.whl
.
File metadata
- Download URL: rabbitmq_decorator-0.0.1-py3-none-any.whl
- Upload date:
- Size: 14.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.9.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2b23a9ea03d2cd650dad5463a8d52667fc9a960694e5e23ea03d750e79f23010 |
|
MD5 | ce397d56beab39360975adcba2b52a81 |
|
BLAKE2b-256 | 5c18ed28107e05894d8a1ae125c2f0468a9f7119002417aa5fe3501b995bc957 |