Home > Backend Development > Python Tutorial > Detailed explanation of RabbitMQ operation graphic code in Python

Detailed explanation of RabbitMQ operation graphic code in Python

黄舟
Release: 2017-05-07 10:47:01
Original
1472 people have browsed it


Knowledge preparation

RabbitMQ

RabbitMQ is a complete, reusable enterprise messaging system based on AMQP.

MQ

MQ stands for Message Queue. Message Queuing (MQ) is an application-to-application communication method.
Detailed explanation of RabbitMQ operation graphic code in Python
Message queue works using publish-subscribe mode. The message sender is the message source. After processing the message, it sends the message to the distributed message queue. The message receiver obtains the message from the distributed message queue and continues processing. It can be seen that there is no direct coupling between the message sender and the message receiver. The message sender ends processing the message when it sends the message to the distributed message queue, while the message receiver only needs to obtain the message from the distributed message queue. Processing without knowing where the message came from.
Through message queue communication, low coupling between service fingers A and B is maintained to achieve flexible business expansion.

pika

pika is the official Python AMQP library written by the RabbitMQ team.

Installation startup

$ brew install rabbitmq$ usr/local/sbin/rabbitmq-server
Copy after login

Installation pika

pip install pika
Copy after login

Operation example

Hello World

sender

# coding: utf-8import pika

connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()

channel.queue_declare(queue='hello')

channel.basic_publish(exchange='',
                      routing_key='hello',
                      body='Hello, World!')print 'send msg: Hello World!'connection.close()
Copy after login

receiver

# coding: utf-8import pika

connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()

channel.queue_declare(queue='hello')def callback(ch, method, properties, body):
    print 'receive msg: %s' % body

channel.basic_consume(callback,
                      queue='hello',
                      no_ack=False)print 'waiting for msg...'channel.start_consuming()
Copy after login

Running result:

# sender.py 运行2次send msg: Hello World!# receiverwaiting for msg...
receive msg: Hello, World!
receive msg: Hello, World!
Copy after login

After the consumer processes the message, it does not exit and can still process subsequent messages.

The above is the detailed content of Detailed explanation of RabbitMQ operation graphic code in Python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template