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 RabbitClient
# These are all the default values if none are provided.
rabbit = RabbitClient(
host='localhost',
port=5672,
default_exchange='',
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 RabbitClient
# Setup
rabbit = RabbitClient(default_exchange='snacks')
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, deserialize=bytes.decode
)
print(f'Received response: {r}')
Output:
Received request: To a python.
Received response: Rabbits and pikas are snacks.
Metadata
Release files for snacks 0.3.6
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| snacks-0.3.6.tar.gz | 8.0 kB | Without provenance | Details for snacks-0.3.6.tar.gz |
Release files / snacks-0.3.6.tar.gz
| Download URL | snacks-0.3.6.tar.gz |
|---|---|
| Size | 8.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
38715ea4ed1f50ade9af9655e19f242446c69edee07543cc6b285bcd3c438fcf
|
|
BLAKE2b-256 checksum How to use checksums |
b141d1a88aa9cc7a4bde498e5fe65aa0a4ac38ca285cd1bf109d49ae102bda11
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.2.0 pkginfo/1.4.2 requests/2.24.0 setuptools/51.0.0 requests-toolbelt/0.8.0 tqdm/4.48.2 CPython/3.8.6
|
Security for snacks 0.3.6
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.