Download 2.5-archived PDF
Transcript
Celery Documentation, Release 2.5.5 ... 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. Note: 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. class celery.contrib.abortable.AbortableAsyncResult(task_id, backend=None, task_name=None, app=None) Represents a abortable result. Specifically, this gives the AsyncResult a abort() 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() Returns True if the task is (being) 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 AbortableTask must call the is_aborted() method periodically and act accordingly when the call evaluates to True. classmethod AsyncResult(task_id) Returns the accompanying AbortableAsyncResult instance. is_aborted(**kwargs) Checks against the backend whether this AbortableAsyncResult is ABORTED. Always returns False in case the task_id parameter refers to a regular (non-abortable) Task. 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). name = ‘celery.contrib.abortable.AbortableTask’ 12.25 celery.contrib.batches Collect messages and processes them as a list. Example A click counter that flushes the buffer every 100 messages, and every 10 seconds. from celery.task import task from celery.contrib.batches import Batches # Flush after 100 messages, or 10 seconds. @task(base=Batches, flush_every=100, flush_interval=10) def count_click(requests): 12.25. celery.contrib.batches 289