Wrapper to pika for an easy to use RabbitMQ interface.
Project description
Snacks
Snacks is a wrapper around pika to provide a convenient interface for RabbitMQ.
Installation
Snacks is available on PyPI and can be installed with:
pip install snacks
Configuration
from snacks.rabbit import RabbitApp
# These are all the default values if none are provided.
rabbit = RabbitApp(
host='localhost',
port=5672,
default_exchange='snacks',
virtual_host='/',
username='guest',
password='guest'
)
Consume From Queues
To decorate a function as a consumer for a queue use the consumer
decorator.
@rabbit.consumer(['queue.name'])
def listen(event: str) -> None:
print(f'Received request: {event}')
Consume From Generated Queues
To decorate a function as a consumer for a generated queue using given
routing keys use the listener
decorator.
@rabbit.listener(['routing_key'])
def listen(event: str) -> None:
print(f'Received request: {event}')
RPC
To make a decorated function a remote procedure add a return.
@rabbit.listener(['routing_key'])
def listen(event: str) -> str:
print(f'Received request: {event}')
return 'Remote response.'
Full Example
from snacks.rabbit import RabbitApp
# Setup
rabbit = RabbitApp()
queue = 'snacks'
key = 'snackey'
rabbit.exchange_declare(exchange_type='topic', durable=True)
rabbit.queue_declare(queue=queue, durable=True)
rabbit.queue_bind(queue=queue, routing_key=key)
@rabbit.consumer([queue])
def listen(event: str) -> str:
print(f'Received request: {event}')
return 'Rabbits and pikas are snacks.'
if __name__ == '__main__':
r = rabbit.publish_and_receive('To a python.', key, serialize=bytes.decode)
print(f'Received response: {r}')
Output:
Received request: To a python.
Received response: Rabbits and pikas are snacks.
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
snacks-0.3.0.tar.gz
(7.2 kB
view hashes)