Use of RABBITMQ in Python (Installation and simple tutorial)

Source: Internet
Author: User

1, Introduction

RabbitMQ (Rabbit message queue) is a popular open source Message Queuing system developed in Erlang language.

1.1 Keyword Description:

Broker: Message Queuing server entity.
Exchange: A message switch that specifies what rules the message is routed to and to which queue.
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 messages are delivered based on this keyword.
Vhost: Virtual host, a broker can open multiple vhost, as a separate user permissions.
Producer: The message producer is the program that delivers the message.
Consumer: The message consumer is the program that receives the message.
Channel: The message channels, in each connection of the client, multiple channels can be established, each channel represents a session task.

1.2 Message Queuing operation mechanism:

(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) Clients post messages to exchange.
(6) When the message is received by Exchange, the message is posted to one or more queues based on the key of the message and the binding already set.

Note: After declaring a queue, if you persist it, you do not need to declare it the next time because the queue is already in RABBITMQ!!!

For example, the following example declares a queue for the first time!!!

1.3exchange Type:

1.Direct switch
Features: Post according to key
For example, the binding set routing key to "Hello", then the client submits the message, only the key is set to "Hello" will be posted to the queue.

2.Topic switch
Features: Post a key pattern match, the symbol "#" matches one or more words, the symbol "*" matches a word
For example, "abc.#" matches "Abc.def.ghi", "abc.*" matches only "Abc.def".

3.Fanout switch
Features: No key required, broadcast mode, a message comes in, post to all queues bound to the switch

2. Build Environment 2.1 Install RABBITMQ in a Windows environment with the following tutorials:

Http://jingyan.baidu.com/article/a17d5285173ce68098c8f2e5.html

2.2 Installing the Pika module

Python uses the RABBITMQ service and can use a ready-made class library Pika, TXAMQP, or Py-amqplib, where Pika is selected.

Use the PIP command directly on the command line:

Pip Install Pika

3. Sample Test

The content of the instance is the message sent from send.py to rabbitmq,receive.py received send.py from RABBITMQ.

P indicates produce, producer's meaning, also can be called sender, the example shows as send.py;

C means consumer, the consumer's meaning, also can be called the receiver, the example shows as receive.py;

The middle red indicates the meaning of the queue, and the instance shows the Hello queue.

send.py

#!/usr/bin/env Python3#-*-coding:utf-8-*-ImportPikaImportRandom#New Connection, RABBITMQ installed locally hostname ' localhost 'hostname ='192.168.8.190'Port=5672Credentials= Pika. Plaincredentials (username='Test', password='Test') Parameters= Pika. Connectionparameters (host=hostname,port=port,credentials=credentials) connection= Pika. Blockingconnection (parameters=parameters)#Create a channelChannel =Connection.channel ()#declare a queue where both the producer and the consumer declare an identical queue to prevent the other party from running in the event that one party hangs upChannel.queue_declare (queue='Hello') number= Random.randint (1, 1000) Body='Hello world:%s'% Number#a queue name indicating which queue the message is sent to; message content#Routing_key is required when using an anonymous switch, indicating which queue to send toChannel.basic_publish (exchange="', routing_key='Hello', body=body)Print "[x] Sent%s"%bodyconnection.close ()

receive.py

#!/usr/bin/env Python3#-*-coding:utf-8-*-ImportPikahostname='192.168.8.190'Port=5672Credentials= Pika. Plaincredentials (username='Test', password='Test') Parameters= Pika. Connectionparameters (host=hostname,port=port,credentials=credentials) connection= Pika. Blockingconnection (parameters=parameters)#Create a channelChannel =Connection.channel () channel.queue_declare (Queue='Hello')defCallback (ch, method, properties, body):Print "[x] Received%r"%(body,)#tell RABBITMQ to use callback to receive informationChannel.basic_consume (Callback, queue='Hello', no_ack=True)#start to receive information, and into the blocking state, there is information in the queue to call callback for processing, press CTRL + C to exitPrint '[*] waiting for messages. To exit Press CTRL + C'channel.start_consuming ()

Let's run send.py first to send a message:

Then run receive.py to receive the message:

Use of RABBITMQ in Python (Installation and simple tutorial)

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.