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
- Suhobrus Boris
- Email: bhobrus@gmail.com
- GitHub: SimpleRabbitMQFastStreamByHobrus
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
hobrus_rabbitmq-0.1.0.tar.gz
(3.2 kB
view hashes)
Built Distribution
Close
Hashes for hobrus_rabbitmq-0.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 102a97d972a1a8ecae93f5c0ddf49a9d50b0ffd2110bcf47e31cdc6f5393b62b |
|
MD5 | a769ac1dfc6c07d9a513274cbd001917 |
|
BLAKE2b-256 | 3494bccbf42bf69544dc894ffe889041d8d14a8e606c5718c4df2645c2265407 |