如果我只有任务ID,可以获取celery任务的参数吗? [英] Can I get a celery task's arguments if all I have is the task ID?

查看:56
本文介绍了如果我只有任务ID,可以获取celery任务的参数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有原始任务,则可以从 task.request.args 中获取参数,但是如果我只有任务ID,是否可以获取参数?看来没有办法从 AsyncResult 对象中获取它们,据我所知,没有办法重新创建任务.

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.

推荐答案

如果任务处于待处理状态或当前正在执行,则可以看到任务的参数.最简单的方法是使用芹菜检查方法.

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()

您可以遍历它们,并使用任务ID,您可以像这样获得所有任务详细信息

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.

这篇关于如果我只有任务ID,可以获取celery任务的参数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆