Getting celery task id

落爺英雄遲暮 提交于 2021-01-29 06:37:10

问题


I have made something like that

@app.task
def some_task()
    logger.info(app.current_task.request.id)
    some_func()

def some_func()
    logger.info(app.current_task.request.id)

So I receive normal id inside some_task, but it equals to None inside some_func. How can I get real task id?


回答1:


You could bind the task and pass the request around rather than relying on a global.

@app.task(bind=True)
def some_task(self)
    logger.info(self.request.id)
    some_func(self.request)

def some_func(celery_request=None)
    # celery_request is optional assuming you're using it elsewhere.
    if celery_request:
        logger.info(celery_request.id)


来源:https://stackoverflow.com/questions/55183581/getting-celery-task-id

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!