PYTHON-RABBITMQ Message Queuing

Source: Internet
Author: User
Tags message queue rabbitmq

Thread queue in Python enables communication between different threads, and process queue enables Python to communicate between different processes

RABBITMQ Message Queuing acts as a middleman, enabling communication between independent processes, and communicating in different programming languages

After the installation of Windows environment is completed RABBITMQ, enter the cmd command services.msc, then open the RABBITMQ service in the service, use RABBITMQ to install the Erlang locale

Installation of RABBITMQ in Ubuntu environment

[Email protected]:~$ sudo apt install rabbitmq-server[email protected]:~$ sudo rabbitmq-server start

RABBITMQ The default listening port is 5672

Send Message End

#-*-Coding:utf-8-*-__author__ = "MuT6 sch01ar" Import pikaconnection = Pika. Blockingconnection (Pika. Connectionparameters (' 127.0.0.1 ')  # establishes a socketchannel = Connection.channel ()  # declares a pipe, Communicate in the pipeline Channel.queue_declare (queue= ' Q ')  # Declare a name Q for Queuechannel.basic_publish in the pipeline (  # Send message                      exchange= ' ,                      routing_key= ' Q ',  # queue name                      body= ' Hello world! ',  # message to send                      ) print (' data sent complete ') Connection.close ()  # Close Queue

Receive Message End

#-*-Coding:utf-8-*-__author__ = "MuT6 sch01ar" Import pikaconnection = Pika. Blockingconnection (Pika. Connectionparameters (' 127.0.0.1 ')) Channel = Connection.channel ()  # declares a pipe, communicates in the pipeline Channel.queue_declare (queue = ' Q ')  # Multiple declaration of a queue to prevent when this program starts before sending the message end times wrong def callback (ch, method, properties, body):    Print (ch, method, properties , body)    # ch is the memory address of the pipe, method is some information about the queue, the body is the message content    print (' Received data: ', body) channel.basic_consume (  # Basic_consume start consuming message                      callback,  # Call the callback function to process the message queue= ' Q ' If the message is received                      ,  # receive the message from the Q queue                      no_ack=true ,  # does not confirm that the message is finished processing, default to False                     ) print (' Start waiting for message ') channel.start_consuming ()  # starts receiving messages, and if no message is stuck here, until there is a message

Open three Receive Message end

Send message end send a message, first open Receive message end first Receive message

Send the message end to send a message, the receiving message is the second open Receive message end, then the third receive message end, and then the first

RABBITMQ will poll for messages

RABBITMQ in the skin directory under the installation directory Rabbitmqctl.bat can view the current queue situation

Rabbitmqctl.bat list_queues

Receive message-side processing messages with the server-side acknowledgment of message processing to prevent the receiving message end when processing messages suddenly cause the message to be lost

Send Message End

#-*-Coding:utf-8-*-__author__ = "MuT6 sch01ar" Import pikaconnection = Pika. Blockingconnection (Pika. Connectionparameters (' 127.0.0.1 ')  # establishes a socketchannel = Connection.channel ()  # declares a pipe, Communicate in the pipeline Channel.queue_declare (queue= ' Q ')  # Declare a name Q for Queuechannel.basic_publish in the pipeline (  # Send message                      exchange= ' ,                      routing_key= ' Q ',  # queue name                      body= ' Hello world! ',  # message to send                      ) print (' data sent complete ') Connection.close ()  # Close Queue

Receive Message End

#-*-Coding:utf-8-*-__author__ = "MuT6 sch01ar" import pikaimport timeconnection = Pika. Blockingconnection (Pika. Connectionparameters (' 127.0.0.1 ')) Channel = Connection.channel ()  # declares a pipe, communicates in the pipeline Channel.queue_declare (queue = ' Q ')  # Multiple declaration of a queue to prevent when this program starts first times than the producer program Wrong DEF callback (ch, method, properties, body):    Print (ch, method, properties , body)    # ch is the memory address of the pipe, method is some information about the queue, the body is the message content    Time.sleep (()    print (' Received data: ', body)    Ch.basic _ack (Delivery_tag=method.delivery_tag)  # with the server-side acknowledgment message has been processed to finish    print (' Message processing complete ') Channel.basic_consume (  # Basic_consume starts consuming message                      callback,  # Call the callback function to process the message                      queue= ' Q ', # receive the message from the Q queue If the message is received                     ) print ( ' Start waiting for message ') channel.start_consuming ()  # starts receiving messages, and if no message is stuck here, until there is a message

Open 3 message receiving end, 1 send message end

Open 3 Receive Message end, waiting to receive message

Send message side send message, first start Receive message end Receive Message

Then turn off the first receive message end, and the second initiated receive message side receives the message

Then the third, and then the first after the third one, unless the message processing is complete

Message persistence

If the message is received properly when the server side (RabbitMQ) is broken, the receiving end of the message will be error, the message will be lost

If you do not want the server side to suddenly disconnect and cause the message to be lost, you can persist the message

Send Message End

#-*-Coding:utf-8-*-__author__ = "MuT6 sch01ar" Import pikaconnection = Pika. Blockingconnection (Pika. Connectionparameters (' 127.0.0.1 ')  # establishes a socketchannel = Connection.channel ()  # declares a pipe, Communicate in the pipeline Channel.queue_declare (queue= ' Q ', durable=true)  # in the pipeline declare a name Q for queue,durable for queue persistence channel.basic_publish (  # Send Message                      exchange= ',                      routing_key= ' Q ',  # queue name                      body= ' Hello world! ',  # message                      to send Properties=pika. Basicproperties (delivery_mode=2)  # make the message persist                      ) print (' Data send Complete ') connection.close ()  # Close Queue

Receive Message End

#-*-Coding:utf-8-*-__author__ = "MuT6 sch01ar" import pikaimport timeconnection = Pika. Blockingconnection (Pika. Connectionparameters (' 127.0.0.1 ')) Channel = Connection.channel ()  # declares a pipe, communicates in the pipeline Channel.queue_declare (queue = ' Q ', durable=true)  # Durable for queue Persistence def callback (ch, method, properties, body):    Print (ch, method, properties, body)    # ch is the memory address of the pipe, the method is some information about the queue, body is the message content    Time.sleep (()    print (' Received data: ', body)    Ch.basic_ ACK (Delivery_tag=method.delivery_tag)  # with the server-side acknowledgment message has been processed to finish    print (' Message processing completed ') Channel.basic_consume (  # Basic_consume starts consuming message                      callback,  # Call the callback function to process the message                      queue= ' Q ', # receive the message from the Q queue If the message is received                     ) print ( ' Start waiting for message ') channel.start_consuming ()  # starts receiving messages, and if no message is stuck here, until there is a message

This way, even if the server is disconnected, the queues and messages are

If the queue and message are not persisted, the queue and message will not be end multiplicity after the server is restarted.

PYTHON-RABBITMQ Message Queuing

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.