Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I get a celery task's arguments if all I have is the task ID?

Tags:

python

celery

If I have the original task I can get the arguments from task.request.args, but if I only have the task ID is there a way to get the arguments? It doesn't look like there is a way to get them from an AsyncResult object, and as far as I can tell there isn't a way to re-create the task.

I want to do this because I have a frontend that polls the backend for updates on tasks, and it would be useful if it could display the task arguments. Seeing as the arguments are stored with the broker, this should be possible, at least when the task is in pending state.

Naturally there are other ways to do this, but it would be a clean way to do things.

like image 211
nathanielobrown Avatar asked Mar 29 '17 12:03

nathanielobrown


1 Answers

If the task is in pending state or if it is executing currently, you can see the arguments of the task. The easiest way is to use celery inspect method.

from celery.task.control import inspect
i = inspect()
active_tasks = i.active()
reserved_tasks = i.reserved()
scheduled_tasks = i.scheduled()

You can iterate over them and by using task id, you can get all the task details like this

{'acknowledged': True,
   'args': '(1000,)',
   'delivery_info': {'exchange': '',
    'priority': 0,
    'redelivered': None,
    'routing_key': 'celery'},
   'hostname': 'celery@pavilion',
   'id': '30d41ba2-3e71-49ce-8e7d-830ba1152256',
   'kwargs': '{}',
   'name': 't.wait',
   'time_start': 1007.945882783,
   'type': 't.wait',
   'worker_pid': 10560}

Alterantively, you can also read data from broker, deseriaze it and you can get the task agruments.

like image 130
Pandikunta Anand Reddy Avatar answered Oct 13 '22 23:10

Pandikunta Anand Reddy