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...')
Config
The RabbitConfig object can easily read from yaml or ini config files.
RabbitConfig.from_yaml_file(Path.cwd().joinpath('rabbit.yaml'))
RabbitConfig.from_ini_file(Path.cwd().joinpath('rabbit.ini'))
Config ini file:
[rabbit]
host = localhost
user = snacks
pass = snacks
port = 5672
exchange_name = default
Config yaml file:
host: localhost
user: snacks
pass: snacks
port: 5672
exchange_name: default
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.0.2.tar.gz
(3.9 kB
view details)
File details
Details for the file snacks-0.0.2.tar.gz
.
File metadata
- Download URL: snacks-0.0.2.tar.gz
- Upload date:
- Size: 3.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/53.0.0 requests-toolbelt/0.9.1 tqdm/4.56.1 CPython/3.9.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ae9e021aeac759a0f05d7e197cd9f4e00db69b9b8a124c46b1253a37fbc2c573 |
|
MD5 | 3adf00081b5068d957050cf05b0ec4c6 |
|
BLAKE2b-256 | bf32178c463e6ece09147bf7a8ca8938db4be3b8862d731e0fc0785434e9fae0 |