Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>A task is a message, and a "periodic task" sends task messages at periodic intervals. Each of the tasks sent will have an unique id assigned to it.</p> <p><code>revoke</code> will only cancel a single task message. To get the id for a task you have to keep track of the id sent, but you can also specify a custom id when you send a task.</p> <p>I'm not sure if you want to cancel a single task message, or if you want to stop the periodic task from sending more messages, so I'll list answers for both.</p> <p>There is no built-in way to keep the id of a task sent with periodic tasks, but you could set the id for each task to the name of the periodic task, that way the id will refer to any task sent with the periodic task (usually the last one). You can specify a custom id this way,</p> <p>either with the <code>@periodic_task</code> decorator:</p> <pre><code>@periodic_task(options={"task_id": "my_periodic_task"}) def my_periodic_task(): pass </code></pre> <p>or with the <code>CELERYBEAT_SCHEDULE</code> setting:</p> <pre><code>CELERYBEAT_SCHEDULE = {name: {"task": task_name, "options": {"task_id": name}}} </code></pre> <p>If you want to remove a periodic task you simply remove the <code>@periodic_task</code> from the codebase, or remove the entry from <code>CELERYBEAT_SCHEDULE</code>. If you are using the Django database scheduler you have to remove the periodic task from the Django Admin interface.</p> <p>PS1: <code>revoke</code> doesn't stop a task that has already been started. It only cancels tasks that haven't been started yet. You can terminate a running task using <code>revoke(task_id, terminate=True)</code>. By default this will send the <code>TERM</code> signal to the process, if you want to send another signal (e.g. KILL) use <code>revoke(task_id, terminate=True, signal="KILL")</code>.</p> <p>PS2: revoke is a remote control command so it is only supported by the RabbitMQ and Redis broker transports. If you want your task to support cancellation you should do so by storing a <code>cancelled</code> flag in a database and have the task check that flag when it starts:</p> <pre><code>from celery.task import Task class RevokeableTask(Task): """Task that can be revoked. Example usage: @task(base=RevokeableTask) def mytask(): pass """ def __call__(self, *args, **kwargs): if revoke_flag_set_in_db_for(self.request.id): return super(RevokeableTask, self).__call__(*args, **kwargs) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload