Nice programing

Celery-현재 작업에 대한 작업 ID 가져 오기

nicepro 2020. 10. 31. 10:05
반응형

Celery-현재 작업에 대한 작업 ID 가져 오기


태스크 내에서 태스크에 대한 task_id 값을 어떻게 얻을 수 있습니까? 내 코드는 다음과 같습니다.

from celery.decorators import task
from django.core.cache import cache

@task
def do_job(path):
    "Performs an operation on a file"

    # ... Code to perform the operation ...

    cache.set(current_task_id, operation_results)

아이디어는 작업의 새 인스턴스를 만들 때 task_id작업 개체 에서을 검색한다는 것입니다. 그런 다음 작업 ID를 사용하여 작업이 완료되었는지 확인합니다. 나는 하지 않습니다 에 의해 작업을 추적 할 path파일이 작업이 완료된 후 "정리"하고, 또는 존재하지 않을 수 있기 때문에 값.

위의 예에서 값은 current_task_id어떻게 습니까?


Celery는 작업이 수락하는 경우 몇 가지 기본 키워드 인수를 설정합니다. (** kwargs를 사용하여 수락하거나 구체적으로 나열 할 수 있습니다.)

@task
def do_job(path, task_id=None):
    cache.set(task_id, operation_results)

기본 키워드 인수 목록은 http://ask.github.com/celery/userguide/tasks.html#default-keyword-arguments에 설명되어 있습니다.


Celery 2.2.0부터 현재 실행중인 작업과 관련된 정보가 저장됩니다 task.request(«컨텍스트»라고 함). 따라서이 컨텍스트에서 작업 ID를 가져와야합니다 (사용되지 않는 키워드 인수가 아님).

@task
def do_job(path):
    cache.set(do_job.request.id, operation_results)

사용 가능한 모든 필드 목록은 http://celery.readthedocs.org/en/latest/userguide/tasks.html?highlight=requestcontext#context에 설명되어 있습니다.


celery 3.1부터 bind데코레이터 인수를 사용하고 현재 요청에 액세스 할 수 있습니다 .

@task(bind=True)
def do_job(self, path):
    cache.set(self.request.id, operation_results)

참고 URL : https://stackoverflow.com/questions/3302320/celery-get-task-id-for-current-task

반응형