码迷,mamicode.com
首页 > 其他好文 > 详细

RabbitMq中的exchange是什么

时间:2015-10-26 00:44:13      阅读:221      评论:0      收藏:0      [点我收藏+]

标签:

在下图中的x便是exchange

技术分享

P是生产者,红色为queue

X可以将P的task进行过滤,从而决定将task做如何处理:例如:

(1),舍弃任务

(2),将任务发送到某个task

(3),将任务发送到所有task

exchange有4中类型:directtopicheaders and fanout

这次主要使用fanout

emit_log.py:task将被发送到exchange

# -*- coding: UTF-8 -*-
import pika

if __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):  # 循环100次发送消息
        channel.basic_publish(exchange="logs", routing_key=‘‘, body=message + " " + str(i),)
    print "sending ", message

receive_log.py

# -*- coding: UTF-8 -*-
import pika

__author__ = ‘Yue‘


def callback(ch, method, properties, body):
    print body


if __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()


RabbitMq中的exchange是什么

标签:

原文地址:http://my.oschina.net/u/2494265/blog/521922

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!