芹菜 - 获取当前任务的任务ID [英] Celery - Get task id for current task

查看:156
本文介绍了芹菜 - 获取当前任务的任务ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从任务中获取任务的task_id值?以下是我的代码:

How can I get the task_id value for a task from within the task? Here's my code:

from celery.decorators import task
from django.core.cache import cache

@task
def do_job(path):
    "Performs an operation on a file"

    # ... Code to perform the operation ...

    cache.set(current_task_id, operation_results)

这个想法是,当我创建一个新的任务实例,我从任务对象中检索 task_id 。然后我使用任务ID来确定任务是否已完成。我不要希望通过路径值跟踪任务,因为该文件在任务完成后被清理,并且可能或者可能不存在。

The idea is that when I create a new instance of the task, I retrieve the task_id from the task object. I then use the task id to determine whether the task has completed. I don't want to keep track of the task by the path value because the file is "cleaned up" after the task completes, and may or may not exist.

在上面的示例中,如何获取 current_task_id

In the above example, how would I get the value of current_task_id?

推荐答案

如果任务接受,芹菜会设置一些默认的关键字参数。
(您可以使用** kwargs接受他们,或者具体列出)

Celery does set some default keyword arguments if the task accepts them. (you can accept them by either using **kwargs, or list them specifically)

@task
def do_job(path, task_id=None):
    cache.set(task_id, operation_results)

默认关键字参数列表如下:
http://ask.github.com/celery/userguide/tasks.html#default-keyword-arguments

The list of default keyword arguments is documented here: http://ask.github.com/celery/userguide/tasks.html#default-keyword-arguments

这篇关于芹菜 - 获取当前任务的任务ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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