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

岁酱吖の 提交于 2019-12-23 09:26:47

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/43093919/can-i-get-a-celery-tasks-arguments-if-all-i-have-is-the-task-id

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!