繁体   English   中英

rabbitmq 中的多个消费者用于多个队列

[英]Multiple consumer in rabbitmq for multiple queue

我有 2 个队列,比如 q1 和 q2,它们对应于 e1 和 e2 交换与绑定密钥 b1 和 b2。 我想并行运行消费者函数,比如 c1 和 c2,它们将分别监听 q1 和 q2。 我尝试了以下方法:

def c1():
    connection = pika.BlockingConnection(pika.ConnectionParameters(host=constants.rmqHostIp))
    channel = connection.channel()
    channel.exchange_declare(exchange='e1', durable='true',
                         type='topic')
    result = channel.queue_declare(durable='false', queue='q1')
    queue_name = result.method.queue
    binding_key = "b1"
    channel.queue_bind(exchange='e1',
                       queue=queue_name,
                       routing_key=binding_key)
    channel.basic_consume(callback,queue=queue_name,no_ack=False)
    channel.start_consuming()

def c2():
    connection = pika.BlockingConnection(pika.ConnectionParameters(host=constants.rmqHostIp))
    channel = connection.channel()
    channel.exchange_declare(exchange='e2', durable='true',
                         type='topic')
    result = channel.queue_declare(durable='false', queue='q2')
    queue_name = result.method.queue
    binding_key = "b2"
    channel.queue_bind(exchange=e1,
                       queue=queue_name,
                       routing_key=binding_key)
    channel.basic_consume(callback,queue=queue_name,no_ack=False)
    channel.start_consuming()

if __name__ == '__main__':
    c1()
    c2()

但是,它只监听 c1 function 和 c2 function,它没有被执行。 如何运行这两个功能? 提前致谢。

编辑:我在 2 个不同的模块(文件)中有方法 c1 和 c1

为了同时运行这两个功能,需要某种顺序的多线程方法。 请在这里查看一些python示例。

这是用Process类修改的代码。 它也可以使用线程或从操作系统中显式运行它。

import pika
from multiprocessing import Process


def callback():
    print 'callback got data'


class c1():
    def __init__(self):
        self.connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
        self.channel = self.connection.channel()
        self.channel.exchange_declare(exchange='e1', durable='true', type='topic')
        result = self.channel.queue_declare(durable='false', queue='q1')
        queue_name = result.method.queue
        binding_key = "b1"
        self.channel.queue_bind(exchange='e1', queue=queue_name, routing_key=binding_key)
        self.channel.basic_consume(callback,queue=queue_name,no_ack=False)

    def run(self):
        self.channel.start_consuming()


class c2():
    def __init__(self):
        self.connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
        self.channel = self.connection.channel()
        self.channel.exchange_declare(exchange='e2', durable='true', type='topic')
        result = self.channel.queue_declare(durable='false', queue='q2')
        queue_name = result.method.queue
        binding_key = "b2"
        self.channel.queue_bind(exchange='e1', queue=queue_name, routing_key=binding_key)

        self.channel.basic_consume(callback,queue=queue_name,no_ack=False)

    def run(self):
        self.channel.start_consuming()

if __name__ == '__main__':
    subscriber_list = []
    subscriber_list.append(c1())
    subscriber_list.append(c2())

    # execute
    process_list = []
    for sub in subscriber_list:
        process = Process(target=sub.run)
        process.start()
        process_list.append(process)

    # wait for all process to finish
    for process in process_list:
        process.join()

您可以使用一个连接和一个通道接收来自多个队列的消息。 pika python 模块具有内置测试代码,可使用一个阻塞连接和一个通道进行测试(仅供参考,此测试代码来自 2015 年)。

这是 pika python 模块测试代码,测试使用一个阻塞连接和一个通道从多个队列获取消息: https://github.com/pika/pika/blob/1.3.0/tests/acceptance/blocking_adapter_test.py#L202 -L2172

ps 由于我自己的顽固原因,我还编写了类似的代码,使用一个阻塞连接、一个通道和两个队列,并验证了它也可以工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM