Created
March 7, 2023 01:05
-
-
Save ebram96/6bff655b0e1b9ad6a55211c41f688e11 to your computer and use it in GitHub Desktop.
Checking whether a Celery task is already running
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from celery import current_app | |
class CeleryHelper: | |
"""Contains helper functionalities to be used while interacting with Celery.""" | |
@staticmethod | |
def is_being_executed(task_name: str) -> bool: | |
"""Returns whether the task with given task_name is already being executed. | |
Args: | |
task_name: Name of the task to check if it is running currently. | |
Returns: A boolean indicating whether the task with the given task name is | |
running currently. | |
""" | |
active_tasks = current_app.control.inspect().active() | |
for worker, running_tasks in active_tasks.items(): | |
for task in running_tasks: | |
if task["name"] == task_name: | |
return True | |
return False |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment