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

后端 未结 2 870
遥遥无期
遥遥无期 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.")
            ...
    
    0 讨论(0)
  • 2020-12-31 14:17

    I actually found this on accident looking for a different issue, but one thing that may help you is on the Basic_Publish function, there is a parameter "Immediate" which is defaulted to False.

    One idea you could do is to set the Immediate Flag to True, which will require it to be consumed by a consumer immediately, instead of sitting in a queue. If a worker is not available to consume the message, it will kick back an error, telling you to start another worker.

    Depending on the throughput of your system, this would either be spawning a lot of extra workers, or spawning workers to replace dead workers. For the former issue you can write an admin-like system that simply tracks workers via a control queue, where you can tell a "Runner" like process to kill processes of workers that are now no longer necessary.

    0 讨论(0)
提交回复
热议问题