【问题标题】:Get current celery task id anywhere in the thread在线程中的任意位置获取当前 celery 任务 id
【发布时间】:2013-12-09 00:42:17
【问题描述】:

我想在正在运行的任务中获取任务 ID, 不知道我在做什么任务。 (这就是为什么我不能使用https://stackoverflow.com/a/8096086/245024

我希望它是这样的:

@task
def my_task():
    foo()

def foo():
    logger.log(current_task_id)

这种模式在许多不同的任务中返回,我不想将任务上下文带到每个内部方法调用中。

一种选择是使用线程本地存储,但是我需要在任务开始之前对其进行初始化,并在完成后对其进行清理。

还有更简单的吗?

【问题讨论】:

  • 你不能用'from celery import current_task; current_task.request.id` ?
  • 这正是我想要的。谢谢!

标签: python celery django-celery celery-task


【解决方案1】:
from celery import current_task
print current_task.request.id

我只是从评论中复制这个,因为它应该是一个答案,所以感谢@asksol。

【讨论】:

    猜你喜欢
    • 2011-03-19
    • 2019-12-29
    • 2018-11-27
    • 1970-01-01
    • 2014-05-08
    • 2021-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多