14
votes

How can I programmatically, using Python code, list current workers and their corresponding celery.worker.consumer.Consumer instances?

3

3 Answers

30
votes

You can use celery.control.inspect to inspect the running workers:

>>> import celery
>>> celery.current_app.control.inspect().ping()
 {u'celery@host': {u'ok': u'pong'}}
12
votes

Short answer:

your_celery_app.control.inspect().stats().keys()

In general that stats() dictionary gives a lot of info. Here's an example value:

{u'broker': {u'alternates': [],
             u'connect_timeout': 4,
             u'heartbeat': 0,
             u'hostname': u'mypcisdabom',
             u'insist': False,
             u'login_method': u'AMQPLAIN',
             u'port': 5672,
             u'ssl': False,
             u'transport': u'amqp',
             u'transport_options': {},
             u'uri_prefix': None,
             u'userid': u'celeryabuser',
             u'virtual_host': u'celeryvhost'},
 u'clock': u'182309',
 u'pid': 1660,
 u'pool': {u'max-concurrency': 1,
           u'max-tasks-per-child': u'N/A',
           u'processes': [2496],
           u'put-guarded-by-semaphore': True,
           u'timeouts': [0, 0],
           u'writes': u'N/A'},
 u'prefetch_count': 4,
 u'rusage': u'N/A',
 u'total': {u'mymodule.my_func': 8}},
1
votes

If you will add --events key when starting. You can check this module for check current workers and etc. http://docs.celeryproject.org/en/latest/userguide/monitoring.html