[Solved]-How to check if celery task is already running before running it again with beat?



No, Celery Beat knows nothing about the running tasks.

One way to achieve what you are trying to do is to link the task to itself. async_apply() for an example has optional parameter link and link_error which can be used to provide a signature (it can be a single task too) to run if the task finishes successfully (link) or unsuccessfully (link_error).

What I use is the following โ€“ I schedule task to run frequently (say every 5 minutes), and I use a distributed lock to make sure I always have only one instance of the task running.

Finally a reminder โ€“ you can always implement your own scheduler, and use it in your beat configuration. I was thinking about doing this in the past for exactly the same thing you want, but decided that the solution I already have is good enough for me.



You can try this
It provides you with a singleton base class for your tasks.

๐Ÿ‘คJoseph Bani


I use Celery with Django models and I implemented a boolean has_task_running at the model level. Then with Celery signals I change the state of the flag to True when signal before_task_publish is trigged and False when a task terminates. Not simple but flexible.


Leave a comment