Package: amqppy
AMQP simplified client for PythonIntroduction to amqppyamqppyis a very simplified AMQP client stacked overPika. It has been tested withRabbitMQ, however it should also work with other AMQP 0-9-1 brokers.The motivation ofamqppyis to provide a very simplified and minimal AMQP client interface which can help Python developers to implement easily messaging patterns such as:Topic Publisher-SubscribersRPC Request-ReplyOthers derivativemessaging patternscan be implemented tunning some parameters of the Topic and Rpc objects.Installing amqppyamqppyis available for download via PyPI and may be installed using easy_install or pip:pip install amqppyTo install from source, run “python setup.py install” in the root source directory.Documentationamqppydocumentation can be found here:https://amqppy.readthedocs.ioTopic Publisher-SubscribersThis is one of the most common messaging pattern where the publisher publishes message to an AMQP exchange and the subscriber receives only the messages that are of interest. The subscriber’s interest is modeled by theTopicor in terms of AMQP by therounting_key.Image from RabbitMQTopic tutorial.Topic SubscriberFirstly, we need to start the Topic Subscriber (also known as Consumer). Inamqppythe classamqppy.consumer.Workerhas this duty.importamqppyBROKER='amqp://guest:guest@localhost:5672//'defon_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=amqppy.Worker(BROKER)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 exceptionworker.run()The subscriber worker will invoke theon_topic_callbackevery time a message is published with a topic that matches with the specifiedrouting_key:‘amqppy.publisher.topic.status’. Note thatrouting_keycan containwildcardstherefore, one subscriber might be listening a set ofTopics.Once the topic subscriber is running we able to launch the publisher.Topic PublisherimportamqppyBROKER='amqp://guest:guest@localhost:5672//'# publish my current statusamqppy.Topic(BROKER).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 Topicrouting_key:‘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 dispatching policy would be applied among subscribers/consumers like happens inwork queues.RPC Request-ReplyThis pattern is commonly known asRemote Procedure CallorRPC. And is widely used when we need to run a functionrequeston a remote computer and wait for the resultreply.Image from RabbitMQRPC tutorialRPC ReplyAn object of typeamqppy.consumer.Workerlistens incomingRPC requestsand computes theRPC replyin theon_request_callback. In the example below, the RPC consumer listens on Requestrounting_key:’amqppy.requester.rpc.division’and the division would be returned as the RPC reply.importamqppyBROKER='amqp://guest:guest@localhost:5672//'defon_rpc_request_division(exchange,routing_key,headers,body):args=json.loads(body)returnargs['dividend']/args['divisor']# subscribe to a rpc request: 'amqppy.requester.rpc.division'worker=Worker(BROKER)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 exceptionworker.run()RPC RequestThe code below shows how to do aRPC Requestusing an instance of classamqppy.publisher.RpcimportamqppyBROKER='amqp://guest:guest@localhost:5672//'# do a Rpc request 'amqppy.requester.rpc.division'result=amqppy.Rpc(BROKER).request(exchange='amqppy.test',routing_key='amqppy.requester.rpc.division',body=json.dumps({'dividend':3.23606797749979,'divisor':2.0}))print('RPC result:{}.'.format(result))