2

I need to cancel a task created by celery. To start the worker, I run:

celery -A myproj -l info -P gevent

I am not 100% sure if it matters, but I am using amazon sqs as the broker.

I came across this article - Cancel an already executing task with Celery?

Since I am calling the task inside my django view, I am also trying to cancel it inside my django view, which looks something like:

import myproj.tasks as tasks

#uncertain as to if apply_async would be any better in this situation...
task = tasks.mytask.delay(...)
#I don't have the luxery to do task.revoke(), but I've tested it anyways with no success
taskid = task.task_id
tasks.app.control.revoke(taskid, terminate=True, signal="SIGKILL")

I check my worker... and it doesn't give me any indication to think that the worker has stopped working on the task, and it still the same amount of time to actually complete the task as it does to kill it (250 seconds)........ and then the "killed task" disrespectfully returns 250 seconds after its completed.

Something else maybe worth mentioning is that the task file imports a class that I've made... so in the task file I have my task defined with the task decorator above the definition, then literally it returns:

return myclass.myclass(**kwargs)

A potential work around I've considered is copying and pasting my class file into my tasks so that I can set a flag inside blocks of the code to check to see if it needs to be aborted or if it needs to continue running... but that seems clunky and intuitively wrong, but maybe I'm wrong.

Any idea as to how I can make this task terminate?


Unfortunate Answer:

You need to use prefork to be able to kill a task as per the comment section. Windows does not support celery, well - you can only use gevent or something to that effect. Prefork is supported by many or all unix / linux based OSs. I had to switch to Redhat Centos 7 to be able to use prefork.

Shmack
  • 1,933
  • 2
  • 18
  • 23
  • 1
    *terminate only supported by prefork*, not gevent. [docs.celeryproject.org/en/stable/userguide/workers.html#revoke-revoking-tasks](https://docs.celeryproject.org/en/stable/userguide/workers.html#revoke-revoking-tasks) – aaron Oct 10 '21 at 03:48
  • Okay, I am reading up on it... the problem that is now posed is that gevent was the "work around" for windows 10, because celery 4.3+ isn't supported for windows 10... – Shmack Oct 10 '21 at 03:55

0 Answers0