S
S
Stanislav Somov2017-12-01 00:23:47
Flask
Stanislav Somov, 2017-12-01 00:23:47

How to interrupt a celery task?

Simple example

from celery import Celery

app = Celery('hello', broker='redis://localhost:6379/0')
id_task=''

@app.task(bind=True)
def testtask(self):
    id_task=str(self.request.id)
    while True:
        pass
    return 'ok'

def stoptask():
    ???? (task_id)

Tried like this
from celery.contrib.abortable import AbortableAsyncResult

stop = AbortableAsyncResult(task_id)
stop.abort()

Nothing happens.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2017-12-01
@DarkDemon

First, the task must be Abortable:

from celery.contrib.abortable import AbortableTask

@app.task(bind=True, base=AbortableTask)
def testtask(self):
    ...

Secondly, instead while True:ofwhile not self.is_aborted():

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question