Can I get celery task arguments if I have a task identifier?

If I have the original task, I can get the arguments from task.request.args, but if I have only the task identifier, is there a way to get the arguments? There doesn't seem to be a way to get them from the object AsyncResult, and as far as I can tell, there is no way to recreate the task.

I want to do this because I have an interface that polls the backend for task updates, and it would be useful if it could display the arguments of the task. Having seen that the arguments are stored by the broker, this should be possible, at least when the task is in a waiting state.

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

+4
source share
1 answer

If the task is in a pending state or if it is currently running, you can see the arguments of the task. The easiest way is to use the celery screening 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 use the task identifier, 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}

In addition, you can also read data from a broker, process it, and you can receive tasks.

+3
source

Source: https://habr.com/ru/post/1673507/


All Articles