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

RabbitMQ的任务分发

时间:2015-10-26 01:58:08      阅读:323      评论:0      收藏:0      [点我收藏+]

标签:

direct分发机制会根据分发关键字(routing_key),将task分发到指定的queue,work只需要监听相应的queue即可,在代码中,需要设置相应的routing_key

fanout机制相反,他会将task分发给所有的queue

fanout模式:

emit_log.py

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

if __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):  # 循环100次发送消息
        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 都将接收到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.start_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()

可以看出两个work均接受到所有的消息

技术分享

技术分享

direct模式:

work的代码只需要将上述代码中的type改为type="direct",并绑定不同的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.start_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(callback,=queue_name,=)
    channel.start_consuming()

从结果中看出:task只分发给了相应的queue

技术分享

技术分享











RabbitMQ的任务分发

标签:

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

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