8

I would like to be able to abort a task that is running from a Celery queue (using rabbitMQ). I call the task using

task_id = AsyncBoot.apply_async(args=[name], name=name, connect_timeout=3)

where AsyncBoot is a defined task.

I can get the task ID (assuming that is the long string that apply_async returns) and store it in a database but I'm unsure how to call an abort method. I see how to make methods abortable with the Abortable tasks class but if I only have the task-id string, how do I call .abort() on the task? Thanks.

Anon
  • 5,103
  • 11
  • 45
  • 58

2 Answers2

12

apply_async returns an AsyncResult instance, or in this case an AbortableAsyncResult. Save the task_id and use that to instantiate a new AbortableAsyncResult later, making sure you supply the backend optional argument if you're not using the default_backend.

abortable_async_result = AsyncBoot.apply_async(args=[name], name=name, connect_timeout=3)
myTaskId = abortable_async_result.task_id

Later:

abortable_async_result = AbortableAsyncResult(myTaskId)
abortable_async_result.abort()
MattH
  • 37,273
  • 11
  • 82
  • 84
  • 1
    It is worth noting that the celery docs say: "this class will only work with the database backends." http://docs.celeryproject.org/en/latest/reference/celery.contrib.abortable.html?highlight=abort#celery.contrib.abortable – dgorissen Nov 08 '11 at 17:03
4

Did you see the reference documentation? http://celeryq.org/docs/reference/celery.contrib.abortable.html

To abort the task use result.abort():

>>> result = AsyncBoot.apply_async(...)
>>> result.abort()
asksol
  • 19,129
  • 5
  • 61
  • 68
  • 2
    But how do I get result object at a later date if all I have is the task_id? I'm trying to abort the task when I no longer have access to the result object. I need someway to pull it from the database. – Anon Aug 30 '10 at 15:12
  • Pickle the result object in the database then – ionelmc Oct 17 '11 at 22:30
  • `mytask.AsyncResult(task_id)` or `from celery.result import AsyncResult; AsyncResult(task_id)`. – asksol Oct 19 '11 at 18:27