Find out whether celery task exists

后端 未结 6 435
离开以前
离开以前 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:17

    You need to call .get() on the AsyncTask object you create to actually fetch the result from the backend.

    See the Celery FAQ.


    To further clarify on my answer.

    Any string is technically a valid ID, there is no way to validate the task ID. The only way to find out if a task exists is to ask the backend if it knows about it and to do that you must use .get().

    This introduces the problem that .get() blocks when the backend doesn't have any information about the task ID you supplied, this is by design to allow you to start a task and then wait for its completion.

    In the case of the original question I'm going to assume that the OP wants to get the state of a previously completed task. To do that you can pass a very small timeout and catch timeout errors:

    from celery.exceptions import TimeoutError
    try:
        # fetch the result from the backend
        # your backend must be fast enough to return
        # results within 100ms (0.1 seconds)
        result = AsyncResult('blubb').get(timeout=0.1)
    except TimeoutError:
        result = None
    
    if result:
        print "Result exists; state=%s" % (result.state,)
    else:
        print "Result does not exist"
    

    It should go without saying that this only work if your backend is storing results, if it's not there's no way to know if a task ID is valid or not because nothing is keeping a record of them.


    Even more clarification.

    What you want to do cannot be accomplished using the AMQP backend because it does not store results, it forwards them.

    My suggestion would be to switch to a database backend so that the results are in a database that you can query outside of the existing celery modules. If no tasks exist in the result database you can assume the ID is invalid.

提交回复
热议问题