本文介绍了在Django-Celery中停止/清除定期任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经设法通过对PeriodicTask进行子类化来定期在django-celery中工作。我试图创建一个测试任务,并设置它运行无用的东西。有用。



现在我无法阻止。我已经阅读了文档,我无法找到如何从执行队列中删除任务。我尝试使用celeryctl和使用shell,但是registry.tasks()是空的,所以我看不到如何删除它。



我已经看到建议我应该撤销它,但对于这一点,我似乎需要一个任务ID,我看不到我将如何找到任务ID。



谢谢。

解决方案

任务是一条消息, 周期性任务周期性地发送任务消息。发送的每个任务将分配给它的唯一ID。



撤销只会取消单个任务消息。要获取任务的ID,您必须保持
跟踪发送的ID,但是您也可以在发送任务时指定自定义ID。



我不知道您是否要取消单个任务消息,或者如果要停止定期任务发送更多消息,那么我将列出两者的答案。



没有内置的方法来保持任务发送的定期任务
,但您可以将每个任务的id设置为定期任务的名称,这样
id将引用与定期任务一起发送的任务(通常是最后一个)。
您可以使用 @periodic_task 装饰器



指定自定义ID:

  @periodic_task(options = {task_id:my_periodic_task} 
def my_periodic_task():
通过

或使用 CELERYBEAT_SCHEDULE 设置: / p>

  CELERYBEAT_SCHEDULE = {name:{task:task_name,
options:{task_id:name}如果你想删除一个定期的任务,你只需删除 $ / code $或者从代码库中删除 CELERYBEAT_SCHEDULE 中的条目。
如果您使用Django数据库调度程序,您必须从Django Admin界面中删除定期任务



PS1: revoke 不会停止已经开始的任务。它只会取消尚未启动的
任务。您可以使用
revoke(task_id,terminate = True)终止正在运行的任务。默认情况下,如果要发送另一个信号(例如KILL)使用
发送 TERM 信号> revoke(task_id,terminate = True,signal =KILL)



PS2:撤销是一个遥控器命令,因此它只是由RabbitMQ
和Redis经纪人运输支持。
如果您希望您的任务支持取消,您应该通过在数据库中存储一个取消的
标志,并将任务检查该标记开始:

  from celery.task import Task 

class RevokeableTask(Task):


示例用法:

@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)


I have managed to get periodic tasks working in django-celery by subclassing PeriodicTask. I tried to create a test task and set it running doing something useless. It works.

Now I can't stop it. I've read the documentation and I cannot find out how to remove the task from the execution queue. I have tried using celeryctl and using the shell, but registry.tasks() is empty, so I can't see how to remove it.

I have seen suggestions that I should "revoke" it, but for this I appear to need a task id, and I can't see how I would find the task id.

Thanks.

解决方案

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.

revoke will only cancel a single task message. To get the id for a task you have to keeptrack of the id sent, but you can also specify a custom id when you send a task.

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.

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 waythe id will refer to any task sent with the periodic task (usually the last one).You can specify a custom id this way,

either with the @periodic_task decorator:

@periodic_task(options={"task_id": "my_periodic_task"})
def my_periodic_task():
    pass

or with the CELERYBEAT_SCHEDULE setting:

CELERYBEAT_SCHEDULE = {name: {"task": task_name,
                              "options": {"task_id": name}}}

If you want to remove a periodic task you simply remove the @periodic_task from the codebase, or remove the entry from CELERYBEAT_SCHEDULE.If you are using the Django database scheduler you have to remove the periodic taskfrom the Django Admin interface.

PS1: revoke doesn't stop a task that has already been started. It only cancelstasks that haven't been started yet. You can terminate a running task usingrevoke(task_id, terminate=True). By default this will send the TERM signal tothe process, if you want to send another signal (e.g. KILL) userevoke(task_id, terminate=True, signal="KILL").

PS2: revoke is a remote control command so it is only supported by the RabbitMQand Redis broker transports.If you want your task to support cancellation you should do so by storing a cancelledflag in a database and have the task check that flag when it starts:

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)

这篇关于在Django-Celery中停止/清除定期任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 05:05