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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file hobrus_rabbitmq-0.1.0.tar.gz.
File metadata
- Download URL: hobrus_rabbitmq-0.1.0.tar.gz
- Upload date:
- Size: 3.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.11.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
159c20a5ef4f3e8db1a03b7b48d72b77abf03004f755c889b572c2a7e0e63a6f
|
|
| MD5 |
f0a8699fd7ecb9cf96a9dcd9bc23bbe0
|
|
| BLAKE2b-256 |
96eb5dcef3b021e17af5c8c7363a269f6ed8c001a7bd5250955541ca22d11fb0
|
File details
Details for the file hobrus_rabbitmq-0.1.0-py3-none-any.whl.
File metadata
- Download URL: hobrus_rabbitmq-0.1.0-py3-none-any.whl
- Upload date:
- Size: 3.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.11.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
102a97d972a1a8ecae93f5c0ddf49a9d50b0ffd2110bcf47e31cdc6f5393b62b
|
|
| MD5 |
a769ac1dfc6c07d9a513274cbd001917
|
|
| BLAKE2b-256 |
3494bccbf42bf69544dc894ffe889041d8d14a8e606c5718c4df2645c2265407
|