Python uses the RabbitMQ server to implement the switch function instance tutorial, pythonrabbitmq

Source: Internet
Author: User

Python uses the RabbitMQ server to implement the switch function instance tutorial, pythonrabbitmq

Let's take a quick look at the setup of the RabbitMQ Server:

sudo apt-get install rabbitmq-server

Python requires the Pika library to use RabbitMQ:

sudo pip install pika

Now let's take a look at the working principle of the switch: the message sender sends the message to the switch first, and then the switch sends the message to the bound message queue, then, each acceptor can receive information from its own message queue.

The following uses send. py and receive. py to simulate the function of the switch. Send. py indicates the sender, and receive. py indicates the receiver.

Receive. py:

#! /Usr/bin/env python # coding = utf8import pika connection = pika. blockingConnection (pika. connectionParameters ('localhost') channel = connection. channel () # define the switch channel. exchange_declare (exchange = 'messages ', type = 'fanout') # generate a random queue and bind it to the switch. result = channel. queue_declare (exclusive = True) queue_name = result. method. queuechannel. queue_bind (exchange = 'messages ', queue = queue_name) def callback (ch, method, properties, body): print "[x] Received % r" % (body,) channel. basic_consume (callback, queue = queue_name, no_ack = True) print '[*] Waiting for messages. to exit press CTRL + C' channel. start_consuming ()

In the code above, the queue_declare parameter exclusive = True indicates that when the receiving end exits, the queue temporarily generated will be destroyed, so that it will not occupy resources. Run this program, and then run the rabbitmqctl list_exchanges command to view the switch information:

The red box is the switch defined in the preceding example. Use rabbitmqctl list_queues to view the message queue status:

The red box is a random message queue.

Send. py:

#! /Usr/bin/env python # coding = utf8import pika connection = pika. blockingConnection (pika. connectionParameters ('localhost') channel = connection. channel () # define the switch channel. exchange_declare (exchange = 'messages ', type = 'fanout') # send messages to the switch channel. basic_publish (exchange = 'messages ', routing_key = '', body = 'Hello World! ') Print "[x] Sent' Hello World! '"Connection. close ()

In the code above, the parameter exchange of the basic_publish method is set as the corresponding switch. Because it is to be broadcast and sent to all queues, The routing_key does not need to be set.

If exchange is blank, it indicates that an anonymous switch is used. In the figure above, you can see a switch like amq. *, which is the default switch of the system. Routing_key must be specified when an anonymous switch is used to indicate the queue to which the switch is sent. The first example demonstrates this function.

Open another terminal and execute send. py. You can see that receive. py received the message. If multiple terminals execute receive. py, each receive. py receives the message.

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.