0

In the django project, I use celery to run asynchronous tasks. I want to implement a function to cancel the running task.

The official documentrevoke: Revoking tasks says that the "revoke(terminate=True)" method can be used. I tried it but it didn't work. Here are some ways I've tried:

...
task = AsyncResult("1e8fb3f3-4253-4bec-b71a-665ba5d23004")
print(task.state)
'STARTED'
task.revoke(terminate=True)
print(task.state)
'STARTED'
app.control.revoke("1e8fb3f3-4253-4bec-b71a-665ba5d23004", terminate=True)
print(task.state)
'STARTED'

And eventually it still executes to completion. Has anyone encountered a similar problem? Or is there another way to achieve my needs in celery? Any help would be greatly appreciated!

911
  • 542
  • 2
  • 12

1 Answers1

0

I encountered same issue in Celery=5.3.0 and this answer helps me to resolve it. It says you should revoke tasks by their results, for example:

 result = add.apply_async(args=[2, 2], countdown=120)
 result.revoke()
Saeed Ramezani
  • 462
  • 6
  • 20
  • @ Saeed Ramezani Thank you for your answer, I did use their results, like this: `from celery.result import AsyncResult`, but it doesn't work. – 911 Jun 25 '23 at 06:12