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