Skip to content

Instantly share code, notes, and snippets.

@ebram96
Created March 7, 2023 01:05
Show Gist options
  • Save ebram96/6bff655b0e1b9ad6a55211c41f688e11 to your computer and use it in GitHub Desktop.
Save ebram96/6bff655b0e1b9ad6a55211c41f688e11 to your computer and use it in GitHub Desktop.
Checking whether a Celery task is already running
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