Python16_day11 "MQ, Redis, Memcache"

Source: Internet
Author: User
Tags message queue rabbitmq

First, 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.

  1.RabbitMQ Install

1 installation configuration Epel source 2    $ RPM-IVH http://dl.fedoraproject.org/pub/epel/6/i386/epel-release-6-8. noarch.rpm3  4  Install Erlang5    $ yum-y install Erlang6  7  Install RABBITMQ8    $ yum-y Install Rabbitmq-server
  

Note: Service Rabbitmq-server start/stop

  2. Python API Install  

1 pip Install Pika 2 or 3 Easy_install Pika 4 or 5 Source Code 6 Https://pypi.python.org/pypi/pika

3. Realization of production consumption model based on queue

1 ImportQueue2 ImportThreading3 4 5Message = Queue.queue (10)6 7 8 defproducer (i):9      whileTrue:Ten message.put (i) One  A  - defConsumer (i): -      whileTrue: themsg =Message.get () -  -  -  forIinchRange (12): +t = Threading. Thread (Target=producer, args=(i,)) - T.start () +  A  forIinchRange (10): att = Threading. Thread (Target=consumer, args=(i,)) -T.start ()

  4. Based on RABBITMQ

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 ImportPika2  3 ########################## producer #########################4  5Connection =Pika. Blockingconnection (Pika. Connectionparameters (6host='localhost'))7Channel =Connection.channel ()8  9Channel.queue_declare (queue='Hello')Ten   OneChannel.basic_publish (exchange="', Arouting_key='Hello', -body='Hello world!') - Print("[x] Sent ' Hello world! '") the connection.close () -  -  -   + ########################### Consumer ########################## -Import Pika +Connection =Pika. Blockingconnection (Pika. Connectionparameters ( Ahost='localhost')) atChannel =Connection.channel () -   -Channel.queue_declare (queue='Hello') -   - defCallback (ch, method, properties, body): -     Print("[x] Received%r"%body) in   - Channel.basic_consume (Callback, toQueue='Hello', +no_ack=True) -   the Print('[*] waiting for messages. To exit Press CTRL + C') *Channel.start_consuming ()

Python16_day11 "MQ, Redis, Memcache"

Related Article

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.