Knowledge preparation RabbitMQRabbitMQ is a complete and reusable enterprise message system based on AMQP. MQMQ is called MessageQueue. message queue (MQ) is a communication method for applications.
Knowledge preparation RabbitMQ
RabbitMQ is a complete and reusable enterprise message system based on AMQP.
MQ
MQ is called Message Queue. MQ is a communication method for applications.
Through message queue communication, services A and B are maintained with low coupling between each other to achieve flexible business expansion.
Pika
Pika is the official Python AMQP library written by the RabbitMQ team.
Install and start
$ brew install rabbitmq$ usr/local/sbin/rabbitmq-server
Install pika
pip install pika
Hello Worldsender
# coding: utf-8import pikaconnection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))channel = connection.channel()channel.queue_declare(queue='hello')channel.basic_publish(exchange='', routing_key='hello', body='Hello, World!')print 'send msg: Hello World!'connection.close()Cycler
# coding: utf-8import pikaconnection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))channel = connection.channel()channel.queue_declare(queue='hello')def callback(ch, method, properties, body): print 'receive msg: %s' % bodychannel.basic_consume(callback, queue='hello', no_ack=False)print 'waiting for msg...'channel.start_consuming()
Running result:
# Sender. py run twice send msg: Hello World! # Receiverwaiting for msg... receive msg: Hello, World! Receive msg: Hello, World!
After the consumer processes the message and does not quit, the consumer can still process the subsequent message.
The above is the detailed description of RabbitMQ operation graphic code in Python. For more information, see other related articles in the first PHP community!