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 Connectionproducers
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 valueConsumer
Python Operation Rabbimq