Skip to main content

Wrapper to pika for an easy to use RabbitMQ interface.

Project description

Snacks

Snacks is a wrapper around pika to provide a convenient interface to publish/subscribe to queues in RabbitMQ.

Example

import os

from pika import BlockingConnection, PlainCredentials
from pika.adapters.blocking_connection import BlockingChannel

from snacks.rabbit import RabbitApp
from snacks.rabbit_config import RabbitConfig

# Initial setup of configuration and Rabbit class.
config = RabbitConfig(
    host='localhost',
    port=5672,
    exchange='snacks',
    virtual_host='/',
    credentials=PlainCredentials(
        os.environ.get('AMQP_USERNAME') or 'guest',
        os.environ.get('AMQP_PASSWORD') or 'guest'
    )
)
rabbit = RabbitApp(config)
# Setup queues to use.
queue = 'snacks'
key = 'snackey'
mq_conn = BlockingConnection(rabbit.config.params)
channel: BlockingChannel = mq_conn.channel()
channel.exchange_declare(
    exchange=rabbit.config.exchange,
    exchange_type='topic',
    durable=True
)
channel.queue_declare(queue=queue, durable=True)
channel.queue_bind(
    exchange=rabbit.config.exchange,
    queue=queue,
    routing_key=key
)


@rabbit.listener([queue])
def listen(event: str) -> None:
    print(f'Received message: {event}')


if __name__ == '__main__':
    rabbit.publish('To a python, rabbits and pikas are snacks.', key)
    try:
        while True:
            pass
    except KeyboardInterrupt:
        print('Exiting...')

Project details


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

Uploaded Source

File details

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

File metadata

  • Download URL: snacks-0.1.0.tar.gz
  • Upload date:
  • Size: 4.9 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.1.0.tar.gz
Algorithm Hash digest
SHA256 6528aba946d3cd6362fba899fe3e6d94d93b04e3a9803c623b855a61ee597c45
MD5 a9a7d809900268fa84ba7e0527d0e581
BLAKE2b-256 1b27f3fb0e8d91277a7d8a03d5a9283fa3947245f2027abdc23736f5875fadbd

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page