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
'Nice programing' 카테고리의 다른 글
4 개의 그룹으로 배열 요소를 작업하는 루비 (0) | 2020.10.31 |
---|---|
org.postgresql.util.PSQLException : FATAL : 죄송합니다. 이미 클라이언트가 너무 많습니다. (0) | 2020.10.31 |
해시가없는 자바 스크립트 창 위치 href? (0) | 2020.10.31 |
git이 오류없이 아무것도 커밋하지 않는 방법? (0) | 2020.10.31 |
Postgres 테이블에 DataFrame을 작성하는 방법은 무엇입니까? (0) | 2020.10.31 |