The x in is the Exchange
P is producer, red is queue
X can filter the task of p to decide what to do with the task: for example:
(1), discard the task
(2) To send tasks to a task
(3), send the task to all task
There are 4 types of exchange:Direct, Topic, Headers andfanout
This time the main use of fanout
Emit_log.py:task will be sent to exchange
#-*-Coding:utf-8-*-import pikaif __name__ = = ' __main__ ': Connection = Pika. Blockingconnection (Pika. Connectionparameters ("localhost")) Channel = Connection.channel () channel.exchange_declare (exchange= "Logs", type= " Fanout ") message =" You are awsome! " For I in range (0, 100): # Loop 100 times Send Message Channel.basic_publish (exchange= "Logs", routing_key= ", Body=message +" "+ S TR (i),) print "Sending", message
receive_log.py
# -*- coding: utf-8 -*-import pika__author__ = ' Yue ' Def callback (ch, method, properties, body): print bodyif __name__ == ' __main__ ': connection= Pika. Blockingconnection (Pika. Connectionparameters ("localhost")) channel=connection.channel () Channel.exchange_declare (exchange= "Logs", type= "Fanout") #随机生成Queue result=channel.queue_declare (exclusive=true) #获取queue的name queue_name=result.method.queue print "Queue_name",queue_name channel.queue_bind (exchange= "Logs", Queue=queue_name) channel.basic_consume ( callback,queue=queue_name,no_ack=true) channel.start_consuming ()
What is Exchange in RABBITMQ