找出celery任务是否存在

找出celery任务是否存在,第1张

找出celery任务是否存在

发送任务时,Celery不会写状态,这部分是一种优化(请参阅http://docs.celeryproject.org/en/latest/userguide/tasks.html#state)。

如果您确实需要它,添加起来很简单:

from celery import current_app# `after_task_publish` is available in celery 3.1+# for older versions use the deprecated `task_sent` signalfrom celery.signals import after_task_publish# when using celery versions older than 4.0, use body instead of headers@after_task_publish.connectdef update_sent_state(sender=None, headers=None, **kwargs):    # the task may not exist if sent using `send_task` which    # sends tasks by name, so fall back to the default result backend    # if that is the case.    task = current_app.tasks.get(sender)    backend = task.backend if task else current_app.backend    backend.store_result(headers['id'], None, "SENT")

然后,您可以测试PENDING状态,以检测是否已(似乎)未发送任务:

>>> result.state != "PENDING"


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5642878.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存