How to check task status in Celery?

前端 未结 13 896
北荒
北荒 2020-11-28 02:44

How does one check whether a task is running in celery (specifically, I\'m using celery-django)?

I\'ve read the documentation, and I\'ve googled, but I can\'t see a

相关标签:
13条回答
  • 2020-11-28 03:28

    I found helpful information in the

    Celery Project Workers Guide inspecting-workers

    For my case, I am checking to see if Celery is running.

    inspect_workers = task.app.control.inspect()
    if inspect_workers.registered() is None:
        state = 'FAILURE'
    else:
        state = str(task.state) 
    

    You can play with inspect to get your needs.

    0 讨论(0)
  • 2020-11-28 03:32

    Every Task object has a .request property, which contains it AsyncRequest object. Accordingly, the following line gives the state of a Task task:

    task.AsyncResult(task.request.id).state
    
    0 讨论(0)
  • 2020-11-28 03:32

    Just use this API from celery FAQ

    result = app.AsyncResult(task_id)
    

    This works fine.

    0 讨论(0)
  • 2020-11-28 03:35

    Old question but I recently ran into this problem.

    If you're trying to get the task_id you can do it like this:

    import celery
    from celery_app import add
    from celery import uuid
    
    task_id = uuid()
    result = add.apply_async((2, 2), task_id=task_id)
    

    Now you know exactly what the task_id is and can now use it to get the AsyncResult:

    # grab the AsyncResult 
    result = celery.result.AsyncResult(task_id)
    
    # print the task id
    print result.task_id
    09dad9cf-c9fa-4aee-933f-ff54dae39bdf
    
    # print the AsyncResult's status
    print result.status
    SUCCESS
    
    # print the result returned 
    print result.result
    4
    
    0 讨论(0)
  • 2020-11-28 03:36

    Try:

    task.AsyncResult(task.request.id).state

    this will provide the Celery Task status. If Celery Task is already is under FAILURE state it will throw an Exception:

    raised unexpected: KeyError('exc_type',)

    0 讨论(0)
  • 2020-11-28 03:38
    res = method.delay()
        
    print(f"id={res.id}, state={res.state}, status={res.status} ")
    
    print(res.get())
    
    0 讨论(0)
提交回复
热议问题