【问题标题】:unable to acces taskid of Celery task in helper fucntion of the same task?无法在同一任务的辅助函数中访问 Celery 任务的任务 ID?
【发布时间】:2019-12-18 23:23:02
【问题描述】:

我正在尝试使用 AsyncResult 在不同的辅助函数中访问 c​​elery 任务的 taskid 但无法访问它

https://celery.readthedocs.io/en/latest/reference/celery.result.html#celery.result.AsyncResult

我在此链接上没有得到任何结果,并尝试使用其他一些链接,例如 https://docs.celeryproject.org/en/latest/userguide/tasks.html

def anotherFunction(data):
    try:
        fvlfn
    except Exception as identifier:
        logging.exception(identifier)


@app.task(bind=True)
def send(self):
    try:
        TASKID = self.request.id
        anotherFunction('no if s')
    except Exception as identifier:
        self.update_state(state='ALMOST DONE')
        logging.exception(self.request.id)


我想在 anotherFunction 中访问 taskid 而不通过它

【问题讨论】:

  • app.current_task.request.id你会在程序的任何地方得到当前的taskid
  • 我认为你想为 Sentry 启用 Celery 集成,而不是做你所做的。 docs.sentry.io/platforms/python/celery那个会在活动页面底部的“附加数据”中附上任务ID。
  • 它附加任务 id,如果任务是从客户端调用的。如果我们只是调用任务而不等待响应,哨兵添加数据将不包含 taskId,我们必须手动添加任务 id,在过滤函数中

标签: python celery scheduled-tasks


【解决方案1】:

您始终可以使用 Celery 应用程序对象来获取有关当前任务的信息:myapp.current_task.request.id(其中 myapp 是 Celery 类的实例)

但是,仅仅因为你可以并不意味着你应该这样做。我们中的许多人更喜欢显式传递函数运行所需的数据,而不是使用一些全局对象。

【讨论】:

    猜你喜欢
    • 2023-03-10
    • 1970-01-01
    • 2011-03-19
    • 2021-12-21
    • 2013-02-18
    • 2016-12-31
    • 1970-01-01
    • 1970-01-01
    • 2023-03-07
    相关资源
    最近更新 更多