How does one check whether a task is running in celery (specifically, I\'m using celery-django)?
I\'ve read the documentation, and I\'ve googled, but I can\'t see a
I found helpful information in the
Celery Project Workers Guide inspecting-workers
For my case, I am checking to see if Celery is running.
inspect_workers = task.app.control.inspect()
if inspect_workers.registered() is None:
state = 'FAILURE'
else:
state = str(task.state)
You can play with inspect to get your needs.
Every Task
object has a .request
property, which contains it AsyncRequest
object. Accordingly, the following line gives the state of a Task task
:
task.AsyncResult(task.request.id).state
Just use this API from celery FAQ
result = app.AsyncResult(task_id)
This works fine.
Old question but I recently ran into this problem.
If you're trying to get the task_id you can do it like this:
import celery
from celery_app import add
from celery import uuid
task_id = uuid()
result = add.apply_async((2, 2), task_id=task_id)
Now you know exactly what the task_id is and can now use it to get the AsyncResult:
# grab the AsyncResult
result = celery.result.AsyncResult(task_id)
# print the task id
print result.task_id
09dad9cf-c9fa-4aee-933f-ff54dae39bdf
# print the AsyncResult's status
print result.status
SUCCESS
# print the result returned
print result.result
4
Try:
task.AsyncResult(task.request.id).state
this will provide the Celery Task status. If Celery Task is already is under FAILURE state it will throw an Exception:
raised unexpected: KeyError('exc_type',)
res = method.delay()
print(f"id={res.id}, state={res.state}, status={res.status} ")
print(res.get())