celery: get function name by task id?

为君一笑 提交于 2021-01-28 04:49:20

问题


I am using celery on_failure handler to logging all failed tasks for debugging and analysis. And I want to know the task name(function name) of the failed task, how can I get that?

from celery import Task

class DebugTask(Task):
    abstract = True

    def after_return(self, *args, **kwargs):
        print('Task returned: {0!r}'.format(self.request))

    def on_failure(self, exc, task_id, args, kwargs, einfo):
        func_name = get_func_name_by_task_id(task_id) # how do I do this?
        print "{} failed".format(func_name)           # expected out: add failed.

@app.task(base=DebugTask)
def add(x, y):
    return x + y

PS: I know there is task_id, but query function name by task_id every time is not fun,


回答1:


Quick look at the documentation shows Task.name.



来源:https://stackoverflow.com/questions/30183602/celery-get-function-name-by-task-id

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