celery.contrib.abortable
The AbortableTask serves as a base class for all Task objects that should support abortion by producers.
- Producers may invoke the abort() method on instances, to request abortion.
- Consumers (workers) should periodically check (and honor!) the is_aborted() method at controlled points in their task’s run() method. The more often, the better.
The necessary intermediate communication is dealt with by the AbortableTask implementation.
In the consumer:
In the producer:
def myview(request):
# async_result is of type AbortableAsyncResult
async_result.abort()
...
After the async_result.abort() call, the task execution is not aborted immediately. In fact, it is not guaranteed to abort at all. Keep checking the async_result status, or call async_result.wait() to have it block until the task is finished.
注解
In order to abort tasks, there needs to be communication between the producer and the consumer. This is currently implemented through the database backend. Therefore, this class will only work with the database backends.
Represents a abortable result.
Specifically, this gives the AsyncResult a method, which sets the state of the underlying Task to ‘ABORTED’.
abort()[源代码]
Set the state of the task to ABORTED.
Abortable tasks monitor their state at regular intervals and terminate execution if so.
Be aware that invoking this method does not guarantee when the task will be aborted (or even if the task will be aborted at all).
is_aborted()
class celery.contrib.abortable.AbortableTask[源代码]
A celery task that serves as a base class for all Task‘s that support aborting during execution.
All subclasses of must call the is_aborted() method periodically and act accordingly when the call evaluates to True.
is_aborted(\*kwargs*)
Checks against the backend whether this AbortableAsyncResult is ABORTED.
Be aware that invoking this method will cause a hit in the backend (for example a database query), so find a good balance between calling it regularly (for responsiveness), but not too often (for performance).