241π
revoke cancels the task execution. If a task is revoked, the workers ignore the task and do not execute it. If you donβt use persistent revokes your task can be executed after workerβs restart.
https://docs.celeryq.dev/en/stable/userguide/workers.html#worker-persistent-revokes
revoke has an terminate option which is False by default. If you need to kill the executing task you need to set terminate to True.
>>> from celery.task.control import revoke
>>> revoke(task_id, terminate=True)
https://docs.celeryq.dev/en/stable/userguide/workers.html#revoke-revoking-tasks
60π
In Celery 3.1, the API of revoking tasks is changed.
According to the Celery FAQ, you should use result.revoke:
>>> result = add.apply_async(args=[2, 2], countdown=120)
>>> result.revoke()
or if you only have the task id:
>>> from proj.celery import app
>>> app.control.revoke(task_id)
- [Django]-Django DRF with oAuth2 using DOT (django-oauth-toolkit)
- [Django]-Format numbers in django templates
- [Django]-How to show a many-to-many field with "list_display" in Django Admin?
38π
@0x00mhβs answer is correct, however recent celery docs say that using the terminate
option is βa last resort for administratorsβ because you may accidentally terminate another task which started executing in the meantime. Possibly a better solution is combining terminate=True
with signal='SIGUSR1'
(which causes the SoftTimeLimitExceeded exception to be raised in the task).
- [Django]-How to move a model between two Django apps (Django 1.7)
- [Django]-How to iterate through dictionary in a dictionary in django template?
- [Django]-Django migration strategy for renaming a model and relationship fields
8π
Per the 5.2.3 documentation, the following command can be run:
celery.control.revoke(task_id, terminate=True, signal='SIGKILL')
where
celery = Celery(app.name, broker=app.config['CELERY_BROKER_URL'])
Link to the doc: https://docs.celeryq.dev/en/stable/reference/celery.app.control.html?highlight=revoke#celery.app.control.Control.revoke
- [Django]-How to select a record and update it, with a single queryset in Django?
- [Django]-Django Cache cache.set Not storing data
- [Django]-Unable to find a locale path to store translations for file __init__.py
6π
You define celery app with broker and backend something like :
from celery import Celery
celeryapp = Celery('app', broker=redis_uri, backend=redis_uri)
When you run send task it return unique id for task:
task_id = celeryapp.send_task('run.send_email', queue = "demo")
To revoke task you need celery app and task id:
celeryapp.control.revoke(task_id, terminate=True)
- [Django]-How do you serialize a model instance in Django?
- [Django]-Django CSRF check failing with an Ajax POST request
- [Django]-Django: sqlite for dev, mysql for prod?
4π
In addition, unsatisfactory, there is another way(abort task) to stop the task, but there are many unreliability, more details, see:
http://docs.celeryproject.org/en/latest/reference/celery.contrib.abortable.html
- [Django]-Include intermediary (through model) in responses in Django Rest Framework
- [Django]-How to Unit test with different settings in Django?
- [Django]-Django admin file upload with current model id
2π
from celery.app import default_app
revoked = default_app.control.revoke(task_id, terminated=True, signal='SIGKILL')
print(revoked)
- [Django]-How to loop over form field choices and display associated model instance fields
- [Django]-Django model constraint for related objects
- [Django]-How to go from django image field to PIL image and back?
1π
See the following options for tasks: time_limit, soft_time_limit (or you can set it for workers). If you want to control not only time of execution, then see expires argument of apply_async method.
- [Django]-Django Installed Apps Location
- [Django]-How to convert a Django QuerySet to a list?
- [Django]-CommandError: You must set settings.ALLOWED_HOSTS if DEBUG is False
0π
from celery.result import AsyncResult
task = AsyncResult(task_id)
task.revoke()
- [Django]-What's the difference between select_related and prefetch_related in Django ORM?
- [Django]-Django-way for building a "News Feed" / "Status update" / "Activity Stream"
- [Django]-How to force application version on AWS Elastic Beanstalk