Python Operation RABBITMQ Series (i)

Source: Internet
Author: User
Tags rabbitmq

From the beginning of this article, we will discuss the related functions of RABBITMQ in the next section. My articles are ultimately going to implement a project (specifically what is not disclosed). Each of the preceding articles is preparing for the system. RABBITMQ, is one of the key parts of our project. So Niu Mei, this series, please be sure to understand how RABBITMQ is going on, and know, how to operate.

In this article, we know rabbitmq simple logic.

Production message:

Consumer News:

Just like QQ, I sent here, not directly to you, but to the middle of the server, you receive also not directly from me, from the server to fetch.

The red part is the queue, the queue is used to buffer the message. In this way, we will not let ourselves be blocked by the constant sending of information on the bilateral side.

Before you begin coding practice. We need to install RABBITMQ server and Python client.

Installing the RABBITMQ Server reference article

Install Python client: use pip install Pika

Once installed, we can try the demo of the official documentation:

Send side:

Import Pika

#连接队列服务器
Connection = Pika. Blockingconnection (Pika. Connectionparameters (host= ' localhost '))
Channel = Connection.channel ()

#创建队列. It doesn't matter, it's not created automatically.
Channel.queue_declare (queue= ' Hello ')

#使用默认的交换机发送消息. If Exchange is empty, use the default
Channel.basic_publish (exchange= ", routing_key= ' hello ', body= ' Hello world! ')
Print ("[x] Sent ' Hello world! '")
Connection.close ()

Message effects received by the server

Client Consumer messages:

Import Pika

# Connection Server
Connection = Pika. Blockingconnection (Pika. Connectionparameters (host= ' localhost '))
Channel = Connection.channel ()

# RABBITMQ The consumer still uses this method to create the queue. This means: if not, create it. and send the truth to the end. The purpose is to ensure that the queue is bound to have
Channel.queue_declare (queue= ' Hello ')


# callbacks after receiving a message
DEF callback (ch, method, properties, body):
Print ("[x] Received%r"% body)


Channel.basic_consume (callback, queue= ' Hello ', no_ack=true)

Print (' [*] waiting for messages. To exit Press CTRL + C ')
Channel.start_consuming ()

Message Received:

Then look back at the server console:

After the message is consumed, the queue is removed accordingly.

Today, let's get started with RABBITMQ below. In the next chapter, we'll discuss tasks that are used to allocate time-consuming between multiple workers. This is especially useful in Web applications where concurrency is relatively high.

Note: The code originates from the official documentation. Because it is English, strong professional, some students look at the arduous, here on the actual use of our understanding, re-elaborated again, I hope it is easier to learn.

Python Operation RABBITMQ Series (i)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.