amqppy is a very simplified AMQP client stacked over Pika
Project description
AMQP simplified client for Python
Introduction to amqppy
amqppy is a very simplified AMQP client stacked over Pika. It has been tested with RabbitMQ, however it should also work with other AMQP 0-9-1 brokers.
The motivation of amqppy is to provide a very simplified and minimal AMQP client interface which can help Python developers to implement easily messaging patterns such as:
Others derivative messaging patterns can be implemented tunning some parameters of the Topic and Rpc objects.
Installing amqppy
amqppy is available for download via PyPI and may be installed using easy_install or pip:
pip install amqppy
To install from source, run “python setup.py install” in the root source directory.
Documentation
amqppy documentation can be found here: https://amqppy.readthedocs.io
Topic Publisher-Subscribers
This is one of the most common messaging pattern where the publisher publishes message to an AMQP exchange and the subscriber sreceive only the messages that are of interest. The subscribers’ interest is modeled by the Topic or in terms of AMQP by the rounting_key.
Image from RabbitMQ Topic tutorial.
Topic Subscriber
Firstly, we need to start the Topic Subscribers or Consumers. In amqppy this task is done by the amqppy.consumer.Worker object.
from amqppy.consumer import Worker
def on_topic_status(exchange, routing_key, headers, body):
print('Received message from topic \'amqppy.publisher.topic.status\': {}'.format(body))
# subscribe to a topic: 'amqppy.publisher.topic.status'
worker = Worker(broker='amqp://guest:guest@localhost:5672//')
worker.add_topic(exchange='amqppy.test',
routing_key='amqppy.publisher.topic.status',
on_topic_callback=on_topic_status)
# it will wait until worker is stopped or an uncaught exception
worker.run()
The subscriber worker will invoke the request_func every time that the published message topic matches with the specified routing_key.
Once the topic subscriber is running we able to launch the publisher.
Topic Publisher
from amqppy.publisher import Topic
# publish my current status
Topic(broker='amqp://guest:guest@localhost:5672//').publish(exchange='amqppy.test',
routing_key='amqppy.publisher.topic.status',
body='RUNNING')
The topic publisher will send a message to the AMQP exchange with the topic ‘amqppy.publisher.topic.status’, therefore, all the subscribed subscribers will receive the message unless they do not share the same queue. In case they share the same queue a round robin delivery policy would be applied among the subscribers.
RPC Request-Reply
This pattern is commonly known as Remote Procedure Call or RPC. And is widely used when we need to run a function request on a remote computer and wait for the result reply.
Image from RabbitMQ RPC tutorial
RPC Reply
An object of type amqppy.consumer.Worker listens incoming RPC requests and computes the RPC reply in the on_request_callback. In the example below, the RPC consumer listens on request ‘amqppy.requester.rpc.division’ and the division is returned as RPC reply.
from amqppy.consumer import Worker
def on_rpc_request_division(exchange, routing_key, headers, body):
args = json.loads(body)
return args['dividend'] / args['divisor']
# subscribe to a rpc request: 'amqppy.requester.rpc.division'
worker = Worker(broker='amqp://guest:guest@localhost:5672//')
worker.add_request(exchange='amqppy.test',
routing_key='amqppy.requester.rpc.division',
on_request_callback=on_rpc_request_division)
# it will wait until worker is stopped or an uncaught exception
worker.run()
RPC Request
The code below shows how to do a Rpc request using amqppy.publisher.Rpc
from amqppy.publisher import Rpc
# do a Rpc request 'amqppy.requester.rpc.division'
result = Rpc(broker='amqp://guest:guest@localhost:5672//').request(exchange='amqppy.test',
routing_key='amqppy.requester.rpc.division',
body=json.dumps({'dividend': 3.23606797749979, 'divisor': 2.0}))
print('RPC result: {}.'.format(result))
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
Built Distribution
Hashes for amqppy-0.0.14-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | cedcf2d8bac5b2920e3c55e73efe7bb700e80b05dd47f0c8968fa0d3ceaa4fc5 |
|
MD5 | a1e33f0fe63f3375c631731317e77667 |
|
BLAKE2b-256 | 3da9873ad4d77cb67630638ffb769d708a04d76314ff8200db8cbb85554dc065 |