RabbitMQ (i)--work Queues

Source: Internet
Author: User

RabbitMQ (i)--work Queues

The main purpose of RABBITMQ using work queues is to avoid resource-intensive tasks, which are different from the way a timed task is handled, but instead encapsulate the task as a message to be added to the queue. While Message Queuing is shared among multiple workers, they are free to pop out data for processing.

Persistent message Durability

In order to guaranteethe loss of messages caused by the unexpected restart of ' RABBITMQ ', the data can be persisted by setting the durable of the message, but it is necessary for both the producer and the consumer to set the persistence to take effect.

It should be noted that 'rabbitmq ' does not allow changing the properties of the message queue that has been created, and if a non-persisted Hello message queue has been created before, an error message is returned.

To set the persistence properties for Message Queuing (second parameter):

Channel.queue_declare (queue='hello', durable=true)

When a message is sent, you need to specify ' Delivery_mode ' for message persistence:

Channel.basic_publish (exchange=", routing_key="task_queue", body=  Message, Properties# make message persistent))
Average Distribution Fair Dispatch

'RABBITMQ ' implements the message sharing function by setting the ' Prefetch_count ' of the ' Basic.qos ' method. It tellsthe producers of ' RABBITMQ ' not to assign too many tasks to a consumer, that is, not to assign new tasks before consumers can handle the tasks they have received.

Channel.basic_qos (Prefetch_count=1)

Where Prefetch_count is the number of tasks that can be processed, and if the upper limit is not reached RABBITMQ will continue to push the task to the consumer.

Instance producer
#!/usr/bin/env python#Coding=utf-8ImportPikaImporttimeconnection=Pika. Blockingconnection (Pika. Connectionparameters (Host='localhost')) Channel=Connection.channel () channel.queue_declare (Queue='Task_queue', durable=True) forIinchRange (100): Message= str (i) +'Hello world!'channel.basic_publish (Exchange="', Routing_key='Task_queue', Body=Message, Properties=pika. Basicproperties (Delivery_mode = 2,#Make message persistent))    Print "[x] Sent%r"%(message,) Time.sleep (1) Connection.close ()
Consumers
#!/usr/bin/env python#Coding=utf-8ImportPikaImporttimeconnection=Pika. Blockingconnection (Pika. Connectionparameters (Host='localhost')) Channel=Connection.channel () channel.queue_declare (Queue='Task_queue', durable=True)Print '[*] waiting for messages. To exit Press CTRL + C'defCallback (ch, method, properties, body):Print "[x] Received%r"%(body,) Time.sleep (2)    Print "[x] done"Ch.basic_ack (Delivery_tag=method.delivery_tag) Channel.basic_qos (Prefetch_count=1) Channel.basic_consume (callback, queue='Task_queue') channel.start_consuming ()

RabbitMQ (i)--work Queues

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.