Python Network programming--RABBITMQ

Source: Internet
Author: User
Tags message queue rabbitmq

One: RABBITMQ Introduction

RABBITMQ is a standard implementation of the AMPQ (Advanced Message Protocol queue). In other words, it is a message queue.

Two: RABBITMQ and thread process queue differences

Thread queue: Cannot cross processes and can only be used for multiple thread data interactions.

Process queue: Used only for parent and child process interactions or to interact with multiple sub-processes belonging to a parent process

What if two different programs or languages, or how different server messages interact? Here you can use the RABBITMQ

Three: Install RABBITMQ

RABBITMQ is developed by the Erlang language, so to install Erlang

1) Erlang Installation

Download: http://erlang.org/download.html Download the source package or win binary package as needed

2) RABBITMQ program installation

Download: http://www.rabbitmq.com/server.html Download the source package or win binary package as needed

3) Python RABBITMQ module installation

Pip Install Pika

Attention:

1) after Windows and Linux installation is complete, the RABBITMQ service is Windows

  

2) If the program runs the following error:pika.exceptions.ConnectionClosed

Description RABBITMQ No installation or RABBITMQ service is not started.

Four: RABBITMQ implementation of the simplest queue communication model

The implementation of the entire RABBITMQ model, in fact, is a lead by the task distribution queue producer and consumer model.

, that is, the producer produces the corresponding information, sends it to the router, and the router, based on the key information in the information,

The process of distributing information to different message queues and then reading the data by consumers to different message queues.

  

  

Description

Broker: The Message Queuing server entity is simply the case.

Exchange; A message switch that specifies what rules a message is routed to that list.

Queue: A message queue carrier in which each message is put into one or more queues.

Binding: Bind, which is the role of binding exchange and queue according to routing rules.

Routing key: The routing keyword, exchange based on this keyword for message delivery.

Vhost: Virtual host, a broker can open multiple vhost, as a separate user permissions.

Prouducer: The message producer is the program that delivers the message.

Conumer: The message consumer is the program that receives the message.

Channel: The message channel, each connection on the client, can establish multiple channels, each channel representing a session task.

The process of using Message Queuing is probably as follows:

1) The client connects to the Message Queuing server and opens a channel.

2) The client declares an exchange and sets the related properties

3) The client declares a queue and sets the related properties

4) The client uses routing key to establish a good binding relationship between Exchange and queue.

5) Client Post message to Exchange

V: simple Example

  

Production and consumption

Send End producer

ImportPika#Create an instanceConnection =Pika. Blockingconnection (Pika. Connectionparameters ('localhost', 5672)#default port 5672, can not write    )#declare a pipeline, send a message in the pipelineChannel =Connection.channel ()#declare a queue in a pipelineChannel.queue_declare (queue='Hello')#RabbitMQ A message can never is sent directly to the queue, it always needs to go through an exchange.Channel.basic_publish (exchange="', Routing_key='Hello',#Queue namebody='Hello world!')#Message ContentPrint("[x] Sent ' Hello world! '") Connection.close ()#Queue shutdown

  

ImportPikaImport Time#Create an instanceConnection =Pika. Blockingconnection (Pika. Connectionparameters ('localhost'))#declaring PipelinesChannel =Connection.channel ()#Why do you declare a ' hello ' queue? #If the determination has been declared, it may not be declared. But you don't know that the machine runs first, so declare it two times. Channel.queue_declare (queue='Hello')defCallback (ch, method, properties, body):#four parameters in the standard format    Print(ch, method, properties)#Print to see what it is        Print("[x] Received%r"%body) Time.sleep (15) Ch.basic_ack (Delivery_tag= Method.delivery_tag)
#and No_ack=false configuration used, indicating that the consumer end processing, eliminate queue messages. If the basic_ack is not set, if the customer is broken or otherwise, the queue message is still present and will be sent to the other consumerChannel.basic_consume (#Consumer NewsCallback#If you receive a message, call the callback function to process the messageQueue='Hello',#you're going to receive messages from that queue. #No_ack=true # If the setup no_ack=true indicates that the queue message is cleared regardless of whether the consumer has finished processing, the default is No_ack=false, which indicates the retention message )Print('[*] waiting for messages. To exit Press CTRL + C') channel.start_consuming ()#Start Consumer News

Six: RabbitMQ message distribution Polling

  Question 1: The above is just a producer, a consumer, can a producer of multiple consumers?

RABBITMQ will use a polling mechanism by default, and distribute the messages sequentially. distribute P messages to individual consumers (c) in turn, similar to load balancing.

2: If the consumer program is not finished running, it is interrupted or the RABBITMQ service is interrupted, and the message in the queue still exists?

Two modes:

If the setting no_ack=true indicates that the message in the queue is cleared regardless of whether the consumer has finished processing.

The default is No_ack=false, which indicates a reserved message. When the consumer interrupts the message, it sends another consumer. When basic_ack (Delivery_tag = Method.delivery_tag) is set to indicate

Indicates that the consumption end is processed, eliminating messages in the queue, and if not set, indicates that the message is still in the queue.

  

  

Python Network programming--RABBITMQ

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.