Abort a task in celery in django

I would like to be able to abort a task that starts from the celery queue (using rabbitMQ). I am invoking a task with

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

where AsyncBoot is a given task.

I can get the task id (assuming it's a long string returned by apply_async ) and store it in the database, but I'm not sure how to call the interrupt method. I see how to make abortable methods with the Abortable task class, but if I only have a line with the task ID, how can I call aabort () in the task? Thanks.

+6
python django celery rabbitmq celery-task
source share
2 answers

apply_async returns an AsyncResult instance AsyncResult or in this case AbortableAsyncResult . Save task_id and use this to create a new AbortableAsyncResult later, making sure you supply an additional argument if you are not using default_backend .

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

Further:

 abortable_async_result = AbortableAsyncResult(myTaskId) abortable_async_result.abort() 
+10
source share

Have you seen the help documentation? http://celeryq.org/docs/reference/celery.contrib.abortable.html

To abort a task, use result.abort() :

 >>> result = AsyncBoot.apply_async(...) >>> result.abort() 
+4
source share

All Articles