Skip to main content

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)

Metadata

Release files for rabbitmq-decorator 0.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for rabbitmq-decorator 0.0.1
File Size Uploaded
rabbitmq-decorator-0.0.1.tar.gz 11.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for rabbitmq-decorator 0.0.1
File Interpreter ABI Platform
rabbitmq_decorator-0.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 25.3 kB

Release files / rabbitmq-decorator-0.0.1.tar.gz

Download URL rabbitmq-decorator-0.0.1.tar.gz
Size 11.4 kB
Tags Source
SHA-256 checksum
How to use checksums
ac1acfc1484e27366b3d79c31b674caaefda66e209eb4d26c43b10795eb7fda3
BLAKE2b-256 checksum
How to use checksums
3f09871e0d5b1c8eb792b2a70b6c70dae3eb3670dd0f3e7293dd02f472a6479f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.13

Release files / rabbitmq_decorator-0.0.1-py3-none-any.whl

Download URL rabbitmq_decorator-0.0.1-py3-none-any.whl
Size 14.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2b23a9ea03d2cd650dad5463a8d52667fc9a960694e5e23ea03d750e79f23010
BLAKE2b-256 checksum
How to use checksums
5c18ed28107e05894d8a1ae125c2f0468a9f7119002417aa5fe3501b995bc957
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.13

Release history Release notifications | RSS feed

This release

0.0.1 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page