Skip to main content

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.

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.6.tar.gz (8.0 kB view details)

Uploaded Source

File details

Details for the file snacks-0.3.6.tar.gz.

File metadata

  • Download URL: snacks-0.3.6.tar.gz
  • Upload date:
  • Size: 8.0 kB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for snacks-0.3.6.tar.gz
Algorithm Hash digest
SHA256 38715ea4ed1f50ade9af9655e19f242446c69edee07543cc6b285bcd3c438fcf
MD5 a8052337da164d8170f71c436f8bf143
BLAKE2b-256 b141d1a88aa9cc7a4bde498e5fe65aa0a4ac38ca285cd1bf109d49ae102bda11

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.3.6 This release

1 file

0.3.5

1 file

0.3.4

1 file

0.3.3

1 file

0.3.2

1 file

0.3.1

1 file

0.3.0

1 file

0.2.1

1 file

0.2.0

1 file

0.1.0

1 file

0.0.5

1 file

0.0.4

1 file

0.0.3

1 file

0.0.2

1 file

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page