Skip to main content

Wrapper to pika for an easier 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

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

from snacks.rabbit import Rabbit
from snacks.rabbit_config import RabbitConfig

# Initial setup of configuration and Rabbit class.
config = RabbitConfig(
    host='localhost',
    port=5672,
    exchange_name='',
    credentials=PlainCredentials('snacks', 'snacks')
)
rabbit = Rabbit(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_name,
    exchange_type='topic',
    durable=True
)
channel.queue_declare(queue=queue, durable=True)
channel.queue_bind(
    exchange=rabbit.config.exchange_name,
    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.0.5.tar.gz (4.1 kB view hashes)

Uploaded Source

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