rabbit-qurator
A decorator for turning a Python function into a RabbitMQ consumer
Description
This library is intended to support Python microservices that need to interface with RabbitMQ. It provides a couple wrappers that can be used to turn functions into RPC style endpoints or fire-and-forget tasks.
Installation
pip install rabbit-qurator
Usage
Consumer
from qurator.queue import Qurator
q = Qurator()
@q.rpc
def do_something(data):
"""
Process data and return a response
:data: dict
:return: dict
"""
return {"message": "Hello"}
- By default this will create a queue called
qurator.do_something
attached to the default exchange.
from kombu import Exchange
from qurator.queue import Qurator
exchange = Exchange('myexchange',
type='direct',
durable=False)
q = Qurator(prefix='my_queue', exchange=exchange)
@q.rpc(queue_name='test_queue')
def do_something(data):
"""
Process data and return a response
:data: dict
:return: dict
"""
return {"message": "Hello"}
- This will create a queue called
my_queue.test_queue
bound tomyexchange
.
Client
I've included a client for posting to the RPC consumer, however it should be possible to use any RPC client.
from qurator.rpc.client import RpcClient
client = RpcClient(exchange=some_exchange)
client.rpc('do_something', {"msg": "Test"})
reply = client.retrieve_messages()
General Notes
Environment
In order to interact with RabbitMQ, you need to be sure that the following environment variables are set when using qurator:
-
RABBITMQ_TRANSPORT_SERVICE_HOST
(default:localhost
) -
RABBITMQ_TRANSPORT_SERVICE_PORT
(default:5672
) -
RABBITMQ_USER
(default:guest
) -
RABBITMQ_PASSWORD
(default:guest
) -
RABBITMQ_VHOST
(default:/
)