Find out whether celery task exists

后端 未结 6 428
离开以前
离开以前 2020-12-13 12:55

Is it possible to find out whether a task with a certain task id exists? When I try to get the status, I will always get pending.

>>> AsyncResult(\'         


        
6条回答
  •  鱼传尺愫
    2020-12-13 13:23

    AsyncResult.state returns PENDING in case of unknown task ids.

    PENDING

    Task is waiting for execution or unknown. Any task id that is not known is implied to be in the pending state.

    http://docs.celeryproject.org/en/latest/userguide/tasks.html#pending

    You can provide custom task ids if you need to distinguish unknown ids from existing ones:

    >>> from tasks import add
    >>> from celery.utils import uuid
    >>> r = add.apply_async(args=[1, 2], task_id="celery-task-id-"+uuid())
    >>> id = r.task_id
    >>> id
    'celery-task-id-b774c3f9-5280-4ebe-a770-14a6977090cd'
    >>> if not "blubb".startswith("celery-task-id-"): print "Unknown task id"
    ... 
    Unknown task id
    >>> if not id.startswith("celery-task-id-"): print "Unknown task id"
    ... 
    

提交回复
热议问题