In Pika or RabbitMQ, How do I check if any consumers are currently consuming?

后端 未结 2 881
遥遥无期
遥遥无期 2020-12-31 13:27

I would like to check if a Consumer/Worker is present to consume a Message I am about to send.

If there isn\'t any Worker, I would start s

2条回答
  •  [愿得一人]
    2020-12-31 14:09

    I was just looking into this as well. After reading through the source and docs I came across the following in channel.py:

    @property
    def consumer_tags(self):
        """Property method that returns a list of currently active consumers
    
        :rtype: list
    
        """
        return self._consumers.keys()
    

    My own testing was successful. I used the following where my channel object is self._channel:

    if len(self._channel.consumer_tags) == 0:
            LOGGER.info("Nobody is listening.  I'll come back in a couple of minutes.")
            ...
    

提交回复
热议问题