[PYTHON] Messaging with AMQP using kombu

About AMQP

AMQP is a protocol for messaging services. Someone thrusts a message into a Queue and someone retrieves the message from the Queue. I understand that.

The following page is very helpful for details. GREE engineer's page Easy to read and very nice.

However, the quality is incomprehensible without moving it, so I experimented. The tools used for the experiment are as follows

The environment construction procedure will be described later.

The screen is from RabbitMQ's Management Plugin.

Make an Exchange

According to the GREE engineer's page, Message is received by ʻExchange, and ʻExchange is passed to Queue.

For the time being, I decided to make it from ʻExchange, and here is what I could see from the [sample code](http://kombu.readthedocs.org/en/latest/reference/kombu.html#exchange) of kombu`.

from kombu import Connection,Exchange

exchange = Exchange('foo_exc', type='direct')

with Connection('amqp://guest:guest@localhost:5672//') as c:
    bound = exchange(c.default_channel)
    bound.declare()

exchange0.png

ʻExchangeis completed. At the bottom,foo_exc` created by the above process.

Create a Queue

Continue to create Queue based on Sample Code.

from kombu import Connection,Exchange,Queue

exchange = Exchange('foo_exc', type='direct')
queue = Queue('bar_queue', exchange=exchange, routing_key='hoge.fuga')

with Connection('amqp://guest:guest@localhost:5672//') as c:
    bound = queue(c.default_channel)
    bound.declare()

queue0.png

Queue is created.

Make and throw a Message

Finally create a Message and plunge into ʻExchange`. See Reference rather than sample code.

from kombu import Connection,Exchange

exchange = Exchange('foo_exc', type='direct')

with Connection('amqp://guest:guest@localhost:5672//') as c:
    bound_exc = exchange(c.default_channel)
    msg = bound_exc.Message("Hello, World")
    bound_exc.publish(msg, routing_key='hoge.fuga')

message0.png

It seems that Hello, World is properly included. Around Get messages.

Take a message

The Message that cannot be taken is just garbage, so take it. Sample code

from kombu import Connection,Exchange,Queue,Consumer

exchange = Exchange('foo_exc', type='direct')
queue = Queue('bar_queue', exchange=exchange, routing_key='hoge.fuga')

def callback(body, message):
    print body
    message.ack()

with Connection('amqp://guest:guest@localhost:5672//') as c:

    with Consumer(c.default_channel, queues=[queue], callbacks=[callback]):
        c.drain_events()

result

ubuntu@ubuntu:~$ python consume.py
Hello, World

Hello, World is displayed.

If you don't call message.ack (), the message will not disappear from Queue.

Environment

RabbitMQ

Built with Docker + docker-compose

docker-compose.yml

rabbit:
    image: rabbitmq:3-management
    hostname: rabbit001
    ports:
        - "15672:15672"
        - "5672:5672"

kombu

Install with pip normally

pip install kombu

Recommended Posts

Messaging with AMQP using kombu
When using optparse with iPython
Try using PythonTex with Texpad.
Using Graphviz with Jupyter Notebook
[S3] CRUD with S3 using Python [Python]
Using Quaternion with Python ~ numpy-quaternion ~
Try using matplotlib with PyCharm
Games using IMU with SenseHat
[Python] Using OpenCV with Python (Basic)
Using a printer with Debian 10
Try using folium with anaconda
Using OpenCV with Python @Mac
Send using Python with Gmail
Complement python with emacs using company-jedi
Harmonic mean with Python Harmonic mean (using SciPy)
[Python] Using OpenCV with Python (Image Filtering)
Japaneseize Matplotlib with Alpine using Docker
Using Rstan from Python with PypeR
ROS Lecture 108 Using Database (mongo) with ROS
[Python] Using OpenCV with Python (Image transformation)
[Python] Using OpenCV with Python (Edge Detection)
Using Sessions and Reflections with SQLAlchemy
Using Lambda with AWS Amplify with Go
Using Japanese with Rodeo's IPython @ Windows
Using cgo with the go command
Notes on using rstrip with python.
Create API using hug with mod_wsgi
Using Chainer with CentOS7 [Environment construction]
Easy with Slack using Bot #NowPlaying
Try using Python's networkx with AtCoder
Using a webcam with Raspberry Pi
When using MeCab with virtualenv python
Precautions when using six with Python 2.5
Using Kali Linux Gui with WSL2