Skip to main content

AMQP simplified client for Python

Version Versions Status Coverage License Documentation Status

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 receives only the messages that are of interest. The subscriber’s interest is modeled by the Topic or in terms of AMQP by the rounting_key.

https://www.rabbitmq.com/img/tutorials/python-five.png

Image from RabbitMQ Topic tutorial.

Topic Subscriber

Firstly, we need to start the Topic Subscriber (also known as Consumer). In amqppy the class amqppy.consumer.Worker has this duty.

import amqppy
BROKER = 'amqp://guest:guest@localhost:5672//'

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 = 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 exception
worker.run()

The subscriber worker will invoke the on_topic_callback every time a message is published with a topic that matches with the specified routing_key: ‘amqppy.publisher.topic.status’. Note that routing_key can contain wildcards therefore, one subscriber might be listening a set of Topics.

Once the topic subscriber is running we able to launch the publisher.

Topic Publisher

import amqppy
BROKER = 'amqp://guest:guest@localhost:5672//'

# publish my current status
amqppy.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 Topic routing_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 in work queues.

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.

https://www.rabbitmq.com/img/tutorials/python-six.png

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 rounting_key:’amqppy.requester.rpc.division’ and the division would be returned as the RPC reply.

import amqppy
BROKER = 'amqp://guest:guest@localhost:5672//'

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)
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 an instance of class amqppy.publisher.Rpc

import amqppy
BROKER = '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))

Metadata

Release files for amqppy 0.0.19

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for amqppy 0.0.19
File Size Uploaded
amqppy-0.0.19.tar.gz 10.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for amqppy 0.0.19
File Interpreter ABI Platform
amqppy-0.0.19-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 24.5 kB

Release files / amqppy-0.0.19.tar.gz

Download URL amqppy-0.0.19.tar.gz
Size 10.2 kB
Tags Source
SHA-256 checksum
How to use checksums
8b695aa54229ecb345ea9bd24c45833ee2cbd74bb04f4ba33861eeea688df85d
BLAKE2b-256 checksum
How to use checksums
37bb4bdc0bec1f9c4064b148cf447aa9dcd66990377e2d4e742e4e61e2ea2f7f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / amqppy-0.0.19-py2.py3-none-any.whl

Download URL amqppy-0.0.19-py2.py3-none-any.whl
Size 14.3 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
ae3b3cdb39cc3feadaf833ce2acdd1c219000456e7f758004ce440b0e45475f1
BLAKE2b-256 checksum
How to use checksums
3e2daa224f769f7089f184b47ec567c546779061e815f393f59f6058f6830825
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.0.19 This release

2 release files

0.0.18

2 release files

0.0.14

2 release files

0.0.13

2 release files

0.0.12

2 release files

0.0.11

2 release files

0.0.10

2 release files

0.0.9

2 release files

0.0.8

2 release files

0.0.7

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page