Skip to main content

A simple rabbitmq client for asyncio applications based on the awesome aio-pika library!

Project description

aio-easy-rabbit

An opinionated way to use RabbitMQ in Asyncio based frameworks.

Installation

to be continued ...

Usage example

Simple consumer example in a FastAPI

from fastapi import FastAPI
from aio_easy_rabbit.connection import (
    connect_to_rabbitmq,
    start_listening_to_queue,
)


app = FastAPI()

@rabbitmq_consumer("test_queue")
async def test_consumer(message):
    print(message)


@app.on_event("startup")
async def startup_event():
    registry = ConsumerRegistry()
    rabbitmq_connection_string = "amqp://guest:guest@localhost"
    app.state.rabbitmq_connection = await connect_to_rabbitmq(
        rabbitmq_connection_string
    )
    for queue_name in registry.get_registered_queues():
        asyncio.create_task(
            start_listening_to_queue(app.state.rabbitmq_connection, queue_name)
        )

Key component is the ConsumerRegistry that will manage state of all registered queues and their consuming functions.

Note: Each decorated consumer will receive a raw string that needs to be serialized as pleased.

Simple publisher example in FastAPI

from fastapi import FastAPI

from aio_easy_rabbit.connection import (
    connect_to_rabbitmq
)
from aio_easy_rabbit.producers import RabbitMQPublisher

app = FastAPI()


class Message(BaseModel):
    message: str


@app.post("/test/{queue_name}/")
async def test_endpoint(queue_name: str, message: Message):
    await app.state.rabbitmq_publisher.publish_message(queue_name, message)
    return {"message": "Message published"}


@app.on_event("startup")
async def startup_event():
    rabbitmq_connection_string = "amqp://guest:guest@localhost"
    app.state.rabbitmq_connection = await connect_to_rabbitmq(
        rabbitmq_connection_string
    )
    app.state.rabbitmq_publisher = RabbitMQPublisher(rabbitmq_connection_string)

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

aio-easy-rabbit-0.0.1.tar.gz (4.3 kB view hashes)

Uploaded Source

Built Distribution

aio_easy_rabbit-0.0.1-py3-none-any.whl (6.1 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page