简体   繁体   English

如何获取计划的芹菜任务的任务对象?

[英]How to get Task objects for scheduled celery tasks?

This question is similar to Retrieve list of tasks in a queue in Celery , but I'd like to obtain the actual Task objects (cf. http://docs.celeryproject.org/en/latest/reference/celery.events.state.html#celery.events.state.Task ) rather than a dictionary representation. 这个问题类似于在Celery中检索队列中的任务列表 ,但我想获取实际的Task对象(请参见http://docs.celeryproject.org/en/latest/reference/celery.events.state .html#celery.events.state.Task )而不是字典表示形式。

When I do 当我做

from celery.task.control import inspect
i = inspect()

and then do i.scheduled() from the shell, I get a result like 然后从外壳执行i.scheduled() ,我得到的结果是

In [75]: i.scheduled()
Out[75]: 
{'celery@Kurts-MacBook-Pro-3.local': [{'eta': '2019-08-01T01:31:37.843141+00:00',
   'priority': 6,
   'request': {'acknowledged': False,
    'args': "[126, 'Business Signup', {'actualCompanyName': 'Gilmore and Beck LLC', 'gigsEnabledRegionMapping': True, 'companyName': 'Gilmore and Beck LLC', 'companyRegionMapping': 'Lesliechester', 'companyId': 'ow9DMA8'}]",
    'delivery_info': {'exchange': '',
     'priority': 0,
     'redelivered': None,
     'routing_key': 'celery'},
    'hostname': 'celery@Kurts-MacBook-Pro-3.local',
    'id': '4ecdc400-8421-4a06-babc-98493362ec67',
    'kwargs': '{}',
    'name': 'backend.tasks.task_send_event_to_iterable',
    'time_start': None,
    'type': 'backend.tasks.task_send_event_to_iterable',
    'worker_pid': None}},
  {'eta': '2019-08-01T01:39:21.205879+00:00',
   'priority': 6,
   'request': {'acknowledged': False,
    'args': "('hyjomuz@mailinator.net',)",
    'delivery_info': {'exchange': '',
     'priority': 0,
     'redelivered': None,
     'routing_key': 'celery'},
    'hostname': 'celery@Kurts-MacBook-Pro-3.local',
    'id': '294910a3-2323-4fcf-9768-115c1a8c5e06',
    'kwargs': '{}',
    'name': 'backend.tasks.task_send_business_lead_notification',
    'time_start': None,
    'type': 'backend.tasks.task_send_business_lead_notification',
    'worker_pid': None}}]}

I would like to do a search through these tasks and conditionally revoke one. 我想对这些任务进行搜索,然后有条件地将其撤消。 However, I would like iterate over the results and have the actual tasks handy, like in the example in How to inspect and cancel Celery tasks by task name . 但是,我想遍历结果并方便使用实际任务,例如“ 如何通过任务名称检查和取消Celery任务”中的示例。 But if I try to do celery.events.state.State() , I get no events: 但是,如果我尝试执行celery.events.state.State() ,则不会收到任何事件:

In [76]: celery.events.state.State()
Out[76]: <State: events=0 tasks=0>

Is it possible to obtain the actual Task objects for scheduled tasks? 是否可以获取计划任务的实际Task对象? In particular, I'm interested in obtaining the args without having to parse or try an ast.literal_eval() . 特别是,我有兴趣获取args而无需解析或尝试ast.literal_eval()

I am not sure you can actually get the actual Task instance, but you can easily create AsyncResult by simply instantiating it with the task ID you want to inspect, and you need (naturally) to pass the Celery application object to it too. 我不确定您是否可以实际获得实际的Task实例,但是可以通过简单地使用要检查的任务ID实例化它来轻松创建AsyncResult ,并且(自然)也需要将Celery应用程序对象传递给它。

Some pseudo-code: 一些伪代码:

from celery.result import AsyncResult
from my.project.celeryapp import myapp

task_res = AsyncResult("9ed888fe-f6b6-4443-85d3-787c5c1b26b0", app=myapp)
print(task_res.state)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM