簡體   English   中英

消耗兩個隊列rabbitmq pika python

[英]consuming two queues rabbitmq pika python

我當前正在使用pika網站上提供的異步示例使用者,並且想知道是否可以讓一個使用者消耗兩個隊列? Rabbitmq站點上的示例似乎僅適合每個隊列一個消費者。

您只需要聲明另一個隊列(QUEUE_2 ='another_queue'),然后修改一些方法。 那里有我修改過的方法:

def on_exchange_declareok(self, unused_frame):
    """Invoked by pika when RabbitMQ has finished the Exchange.Declare RPC
    command.

    :param pika.Frame.Method unused_frame: Exchange.DeclareOk response frame

    """
    LOGGER.info('Exchange declared')
    self.setup_queue(self.QUEUE)
    self.setup_queue(self.QUEUE_2)

def setup_queue(self, queue_name):
    """Setup the queue on RabbitMQ by invoking the Queue.Declare RPC
    command. When it is complete, the on_queue_declareok method will
    be invoked by pika.

    :param str|unicode queue_name: The name of the queue to declare.

    """
    LOGGER.info('Declaring queue %s', queue_name)

    self._channel.queue_declare(self.on_queue_declareok, self.QUEUE)
    self._channel.queue_declare(self.on_queue_declareok, self.QUEUE_2)


def on_queue_declareok(self, method_frame):
    """Method invoked by pika when the Queue.Declare RPC call made in
    setup_queue has completed. In this method we will bind the queue
    and exchange together with the routing key by issuing the Queue.Bind
    RPC command. When this command is complete, the on_bindok method will
    be invoked by pika.

    :param pika.frame.Method method_frame: The Queue.DeclareOk frame

    """
    LOGGER.info('Binding %s to %s with %s',
                self.EXCHANGE, self.QUEUE, self.ROUTING_KEY)
    self._channel.queue_bind(self.on_bindok, self.QUEUE,
                             self.EXCHANGE, self.ROUTING_KEY)
    LOGGER.info('Binding %s to %s with %s',
                self.EXCHANGE, self.QUEUE_2, self.ROUTING_KEY)
    self._channel.queue_bind(self.on_bindok, self.QUEUE_2,
                             self.EXCHANGE, self.ROUTING_KEY)


def start_consuming(self):
    """This method sets up the consumer by first calling
    add_on_cancel_callback so that the object is notified if RabbitMQ
    cancels the consumer. It then issues the Basic.Consume RPC command
    which returns the consumer tag that is used to uniquely identify the
    consumer with RabbitMQ. We keep the value to use it when we want to
    cancel consuming. The on_message method is passed in as a callback pika
    will invoke when a message is fully received.

    """
    LOGGER.info('Issuing consumer related RPC commands')
    self.add_on_cancel_callback()
self._chalnnel.basic_qos(prefetch_count=1)
self._consumer_tag = self._channel.basic_consume(self.on_message,
                                                 self.QUEUE)

self._consumer_tag = self._channel.basic_consume(self.on_message,
                                                 self.QUEUE_2)  

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM