Detect whether Celery is Available/Running

后端 未结 7 524
遥遥无期
遥遥无期 2020-12-04 12:09

I\'m using Celery to manage asynchronous tasks. Occasionally, however, the celery process goes down which causes none of the tasks to get executed. I would like to be able t

7条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-04 12:30

    One method to test if any worker is responding is to send out a 'ping' broadcast and return with a successful result on the first response.

    from .celery import app  # the celery 'app' created in your project
    
    def is_celery_working():
        result = app.control.broadcast('ping', reply=True, limit=1)
        return bool(result)  # True if at least one result
    

    This broadcasts a 'ping' and will wait up to one second for responses. As soon as the first response comes in, it will return a result. If you want a False result faster, you can add a timeout argument to reduce how long it waits before giving up.

提交回复
热议问题