문제

I'd like to get the task id inside a running task, without knowing which task I'm in. (That's why I can't use https://stackoverflow.com/a/8096086/245024)

I'd like it to be something like this:

@task
def my_task():
    foo()

def foo():
    logger.log(current_task_id)

This pattern returns in many different tasks, and I don't want to carry the task context to every inner method call.

One option could be to use the thread local storage, but then I will need to initialize it before the task starts, and clean it after it finished.

Is there something simpler?

도움이 되었습니까?

해결책

from celery import current_task
print current_task.request.id

I'm just copying this from the comment, because it should be an answer, so thanks to @asksol.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top