Download Celery Documentation

Transcript
Celery Documentation, Release 3.0.24
>>> state(’PROGRESS’) > state(STARTED)
True
>>> state(’PROGRESS’) > state(’SUCCESS’)
False
2.13.26 celery.contrib.abortable
• Abortable tasks overview
– Usage example
Abortable tasks overview
For long-running Task‘s, it can be desirable to support aborting during execution. Of course, these tasks should be
built to support abortion specifically.
The AbortableTask serves as a base class for all Task objects that should support abortion by producers.
• Producers may invoke the abort() method on AbortableAsyncResult 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.
Usage example
In the consumer:
from celery.contrib.abortable import AbortableTask
from celery.utils.log import get_task_logger
logger = get_logger(__name__)
class MyLongRunningTask(AbortableTask):
def run(self, **kwargs):
results = []
for x in xrange(100):
# Check after every 5 loops..
if x % 5 == 0: # alternatively, check when some timer is due
if self.is_aborted(**kwargs):
# Respect the aborted status and terminate
# gracefully
logger.warning(’Task aborted.’)
return
y = do_something_expensive(x)
results.append(y)
logger.info(’Task finished.’)
return results
In the producer:
2.13. API Reference
269