Detailed description of RabbitMQ operation graphic code in Python

Source: Internet
Author: User
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!

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.