Celery - Get task id for current task

后端 未结 3 934
小蘑菇
小蘑菇 2020-12-08 03:36

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

@ta         


        
3条回答
  •  执念已碎
    2020-12-08 04:17

    Since Celery 2.2.0, information related to the currently executed task is saved to task.request (it's called «the context»). So you should get task id from this context (not from keyword arguments, which are deprecated):

    @task
    def do_job(path):
        cache.set(do_job.request.id, operation_results)
    

    The list of all available fields is documented here: http://celery.readthedocs.org/en/latest/userguide/tasks.html?highlight=requestcontext#context

提交回复
热议问题