Reputation: 1471
For my web application I use celery
with redis
backend. Official FAQ purposes the following way to acquire the result of a task if I have the ID of the task.
result = my_task.AsyncResult(task_id)
result.get()
I can easily access arguments within the task body:
@app.task
def my_task(foo, bar, baz=None):
kwargs = self.request.kwargs
args = self.request.args
Is there a way to get args
and kwargs
from the AsyncResult or wherever just having the task id?
Upvotes: 7
Views: 11116
Reputation: 712
For new Celery versions you can activate "result_extended" and get args from task object:
app.conf.update(
result_extended=True
)
Then, just:
print(task.args)
Upvotes: 5
Reputation: 29524
If the task is in pending state or if it is executing currently, you can see the arguments of the task as mentioned here.
If you want to get the arguments of a failed tasks, you can setup a custom base task with on_failure
handler to get args when tasks are failed.
from celery import Task
class CustomBaseTask(Task):
def on_failure(self, exc, task_id, args, kwargs, einfo):
print(args)
@app.task(base=CustomBaseTask)
def add(x, y):
raise error
On the other hand, if your task is executed successfully, you will have only result and you can't get back your arguments as that information have been consumed and processed by workers from the brokers.
Upvotes: 3