Skip to main content

Simple RabbitMQ wrapper using FastStream for easy message publishing and subscribing in Python

Project description

Hobrus RabbitMQ

Simple RabbitMQ wrapper using FastStream for easy message publishing and subscribing in Python.

Installation

pip install hobrus-rabbitmq

Environment Variables

The package uses the following environment variables (with default values):

RABBITMQ_USERNAME=guest
RABBITMQ_PASSWORD=guest
RABBITMQ_HOST=localhost
RABBITMQ_PORT=5672
RABBITMQ_PROTOCOL=amqp

You can set these variables in your environment or use a .env file.

Usage

Synchronous Message Publishing

from hobrus_rabbitmq import send_message_sync

# Send a message synchronously
send_message_sync("queue_name", "Hello, World!")

Asynchronous Message Publishing

import asyncio
from hobrus_rabbitmq import send_message

async def publish_message():
    await send_message("queue_name", "Hello, World!")

# Run the async function
asyncio.run(publish_message())

Message Subscribing

from hobrus_rabbitmq import subscriber, run

# Define a message handler
@subscriber("queue_name")
async def process_message(message: str):
    print(f"Received message: {message}")

# Start the subscriber
if __name__ == "__main__":
    run()

Complete Examples

Async Publisher Example

import asyncio
from hobrus_rabbitmq import send_message

async def test_async():
    for i in range(5):
        message = f"Async message {i}"
        print(f"Sending: {message}")
        await send_message("test_queue", message)
        await asyncio.sleep(1)  # Small delay between messages

if __name__ == "__main__":
    asyncio.run(test_async())

Sync Publisher Example

from hobrus_rabbitmq import send_message_sync
import time

def test_sync():
    for i in range(5):
        message = f"Sync message {i}"
        print(f"Sending: {message}")
        send_message_sync("test_queue", message)
        time.sleep(1)  # Small delay between messages

if __name__ == "__main__":
    test_sync()

Subscriber Example

from hobrus_rabbitmq import subscriber, run

@subscriber("test_queue")
async def process_message(message: str):
    print(f"Received message: {message}")

if __name__ == "__main__":
    print("Starting subscriber...")
    run()

Dependencies

  • faststream[rabbit]

License

This project is open-source.

Author

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

hobrus_rabbitmq-0.1.0.tar.gz (3.2 kB view hashes)

Uploaded Source

Built Distribution

hobrus_rabbitmq-0.1.0-py3-none-any.whl (3.5 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