Python Operation Rabbimq

Source: Internet
Author: User

RABBITMQ is a complete, reusable enterprise messaging system based on AMQP. He follows the Mozilla Public License open source agreement.

MQ is all called the message queue, and Message Queuing (MQ) is an application-to-application communication method. Applications communicate by reading and writing messages to and from the queue (data for the application), without requiring a dedicated connection to link them. Message passing refers to the process of communicating between programs by sending data in a message, rather than by directly invoking each other, and directly invoking techniques such as remote procedure calls. Queuing refers to an application communicating through a queue. The use of queues removes the requirement that both the receiving and sending applications execute concurrently.

the module name for the Puthon operation RABBITMQ is: PikaHttps://github.com/pika/pika understand the producer consumer model through queue before operating RABBITMQ
1 #!/usr/bin/env python2 #_*_coding:utf-8 _*_3 4 ImportQueue5 ImportThreading6 7Message = Queue.queue (10)8 9 Ten defProducter (i): One  A      whileTrue: - message.put (i) -  the defConsumer (i): -      whileTrue: - Message.get () -  +  -  +  forIinchRange (5): AW = Threading. Thread (target=producter,args=(i,)) at W.start () -  -  forIinchRange (2): -W = Threading. Thread (target=consumer,args=(i,)) -W.start ()
Producer Consumer Model

For RABBITMQ, production and consumption no longer target a queue object in memory, but rather a message queue implemented by RABBITMQ server on a single server.

1 #!/usr/bin/env python2 #_*_coding:utf-8 _*_3 4 ImportPika5 6Connection = Pika. Blockingconnection (Pika. Connectionparameters (host='192.168.1.108'))#Connecting a RABBITMQ7 8Channel = Connection.channel ()#Create a channel9 TenChannel.queue_declare ("Chenchao")#declaring a message queue One  A  -Channel.basic_publish (exchange="', routing_key="Chenchao", body="fucking!")#Send Message -  the Print "sent fucking world!!!!" -  -Connection.close ()#Close Connection
producers
1 #!/usr/bin/env python2 #_*_coding:utf-8 _*_3 4 ############################# #消费者 ########################5 6 ImportPika7 8Connection = Pika. Blockingconnection (Pika. Connectionparameters (host='192.168.1.108'))9 TenChannel =Connection.channel () One  AChannel.queue_declare ("Chenchao") -  - defCallback (ch, method, properties, body):#fixed format must have 4 parameters the     PrintBody -  -  -Channel.basic_consume (callback,queue="Chenchao", no_ack=true)#callback the data fetched from the queue to the callback method +  -Channel.start_consuming ()#start to take value
Consumer

Python Operation Rabbimq

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.