Here's how you can get the number of messages in a queue using celery, which is an intermediary agent.
Using connection_or_acquire , you can minimize the number of open connections to your broker by using the union of internal celery connections.
celery = Celery(app) with celery.connection_or_acquire() as conn: conn.default_channel.queue_declare( queue='my-queue', passive=True).message_count
You can also extend Celery to provide this feature:
from celery import Celery as _Celery class Celery(_Celery) def get_message_count(self, queue): ''' Raises: amqp.exceptions.NotFound: if queue does not exist ''' with self.connection_or_acquire() as conn: return conn.default_channel.queue_declare( queue=queue, passive=True).message_count celery = Celery(app) num_messages = celery.get_message_count('my-queue')
source share