2014-09-13 11 views
5

Mam to zadanie Seler:Jak ręcznie oznaczyć zadanie selera jako wykonane i ustawić jego wynik?

@app.task 
def do_something(with_this): 

    # instantiate a class from a third party library 
    instance = SomeClass() 

    # this class uses callbacks to send progress info about 
    # the status and progress of what we're doing 
    def progress_callback(data): 
     # this status will change to 'finished' later 
     # but the return value that I want as the task result won't be returned 
     # so this is where I should mark the task as done manually 
     if data['status'] == 'working': 
      # I create a custom state for this task 
      do_something.update_state(
       state = 'PROGRESS', 
       meta = data['progress'] 
      ) 

    # adding the callback to the instance 
    instance.add_callback(progress_callback) 

    # use the instance to do what I want 
    # this functions returns a value that I don't want as the task result 
    # so leaving this function without a return statement will make it None 
    instance.do_job(with_this) 

Jak mogę oznaczyć zadanie jako wykonane ręcznie?

W tym przypadku funkcja osiąga koniec bez instrukcji return, więc task.result otrzymuję None, chcę ustawić dane przekazane do funkcji wywołania zwrotnego jako wynik i oznaczyć zadanie jako wykonane.

Próbowałem za pomocą:

app.backend.mark_as_done(do_something.request.id, data) 

To skutecznie ustawiając stan i wynik zadania, ale później wynik jest ustawiony na wartości zwracanej przez funkcję, która jest tutaj None.

Odpowiedz

5

końcu znalazłem rozwiązanie, które przechowuje stan zadania i doprowadzić następnie ignorując zadania poprzez podniesienie wyjątek Ignore, na przykład:

from celery.exceptions import Ignore 

@app.task 
def do_something(with_this): 

    # store the state and result manually 
    # the SUCCESS state is set by this method 
    app.backend.mark_as_done(
     do_something.request.id, 
     the_data_to_store 
    ) 

    # we can also use update_state which calls 
    # backend.store_result just like mark_as_done 
    # but we have to set the state in this case 
    do_something.update_state(
     state = celery.states.SUCCESS, 
     meta = the_data_to_store 
    ) 

    # ignore the task so no other state is recorded 
    # like what was happening with my function in the question 
    # the task will still be acknowledged 
    raise Ignore() 

Jest to przydatne, gdy nie można przywrócić dane, które chcesz zapisz w wyniku.

Powiązane problemy