RABBITMQ's Task distribution

Source: Internet
Author: User

The direct distribution mechanism distributes the task to the specified queue,work based on the distribution keyword (routing_key) and only needs to listen to the corresponding queue, and in the code, it needs to set the corresponding Routing_key

Fanout mechanism instead, he will distribute the task to all the queue

Fanout mode:

emit_log.py

# -*- coding: utf-8 -*-import pikaif __name__ ==  ' __main__ ':     connection = pika. Blockingconnection (Pika. Connectionparameters ("localhost"))     channel = connection.channel ()      channel.exchange_declare (exchange= "Logs2", type= "direct")     message =   "you are awsome!"     for i in range (0, 100):  #  Loop 100-time Send Message          if i%2==0:             channel.basic_publish (exchange= "Logs2",  routing_key= ' even ',  body=message +   " "  + str (i),)         else:             channel.basic_publish (exchange= "Logs2",  routing_key = ' old ', body=message +  "  " + str (i),)     print " sending  ", message      #两个receive_log   will receive the task

receive_log.py

pika__author__ = Callback (,,, body): body__name__ = =: Connection=pika. Blockingconnection (Pika. Connectionparameters ()) Channel=connection.channel () channel.exchange_declare (=,=) Result=channel.queue_declare (= ) Queue_name=result. Queue, Queue_name channel.queue_bind (=,=queue_name) channel.basic_consume (callback,=queue_name,=) Channel.star T_consuming ()

receive_log2.py

pika__author__ = Callback (,,, body): body__name__ = =: Connection=pika. Blockingconnection (Pika. Connectionparameters ()) Channel=connection.channel () channel.exchange_declare (=,=) Result=channel.queue_declare (= ) Queue_name=result.method.queue, Queue_name channel.queue_bind (=,=queue_name) Channel.basic_consume (callback, =queue_name,=) channel.start_consuming ()

You can see that all two jobs are receiving all the messages.

Direct mode:

The work code only needs to change the type in the above code to type= "direct" and bind different exchange.


pika__author__ = __name__ = =: Connection = Pika. Blockingconnection (Pika.            Connectionparameters ()) channel = Connection.channel () channel.exchange_declare (=,=) message = I (,): i%==:    Channel.basic_publish (=, =, =message + + (i),): channel.basic_publish (=, =, =message + + (i),) , message

receive_even_log.py

pika__author__ = Callback (,,, body): body__name__ = =: Connection=pika. Blockingconnection (Pika. Connectionparameters ()) Channel=connection.channel () channel.exchange_declare (=,=) Result=channel.queue_declare (= ) Queue_name=result. Queue, Queue_name channel.queue_bind (=,=queue_name,=) channel.basic_consume (callback,=queue_name,=) channel.st Art_consuming ()

receive_old_log.py

pika__author__ = Callback (,,, body): body__name__ = =: Connection=pika. Blockingconnection (Pika. Connectionparameters ()) Channel=connection.channel () channel.exchange_declare (=,=) Result=channel.queue_declare (= ) Queue_name=result.method.queue, Queue_name channel.queue_bind (=,=queue_name,=) Channel.basic_consume (Callbac k,=queue_name,=) channel.start_consuming ()

As seen from the results: The task was distributed only to the corresponding queue











RABBITMQ's Task distribution

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.