最佳答案
如何从任务内部获取任务的 task _ id 值:
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 确定任务是否已完成。我 不要希望通过 path
值跟踪任务,因为文件在任务完成后被“清理”,可能存在,也可能不存在。
在上面的例子中,如何得到 current_task_id
的值?