如何向Celery动态添加/删除定期任务(celerybeat) [英] How to dynamically add / remove periodic tasks to Celery (celerybeat)

查看:207
本文介绍了如何向Celery动态添加/删除定期任务(celerybeat)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我定义的函数如下:

If I have a function defined as follows:

def add(x,y):
  return x+y

是否可以动态地将此函数添加为芹菜PeriodicTask并在运行时启动?我想能够做类似(伪代码)的事情:

Is there a way to dynamically add this function as a celery PeriodicTask and kick it off at runtime? I'd like to be able to do something like (pseudocode):

some_unique_task_id = celery.beat.schedule_task(add, run_every=crontab(minute="*/30"))
celery.beat.start(some_unique_task_id)

我还想使用(伪代码)之类的东西来动态停止或删除该任务:

I would also want to stop or remove that task dynamically with something like (pseudocode):

celery.beat.remove_task(some_unique_task_id)

celery.beat.stop(some_unique_task_id)

我不使用djcelery ,它使您可以通过django管理员管理定期任务。

FYI I am not using djcelery, which lets you manage periodic tasks via the django admin.

推荐答案

不,很抱歉,使用

但是做你想做的事很容易扩展,例如django-celery
调度程序只是一个将调度写入数据库
的子类(顶部有一些优化)。

But it's easily extensible to do what you want, e.g. the django-celery scheduler is just a subclass reading and writing the schedule to the database (with some optimizations on top).

甚至可以将django-celery调度程序用于非Django项目。

Also you can use the django-celery scheduler even for non-Django projects.

像这样的事情:


  • 安装django + django-celery:

  • Install django + django-celery:

$ pip install -U django django-celery

$ pip install -U django django-celery

将以下设置添加到celeryconfig中:

Add the following settings to your celeryconfig:

DATABASES = {
    'default': {
        'NAME': 'celerybeat.db',
        'ENGINE': 'django.db.backends.sqlite3',
    },
}
INSTALLED_APPS = ('djcelery', )


  • 创建数据库表:

  • Create the database tables:

    $ PYTHONPATH=. django-admin.py syncdb --settings=celeryconfig
    


  • 使用数据库调度程序启动celerybeat:

  • Start celerybeat with the database scheduler:

    $ PYTHONPATH=. django-admin.py celerybeat --settings=celeryconfig \
        -S djcelery.schedulers.DatabaseScheduler
    


  • 还有 djcelerymon 命令,该命令可用于非Django项目
    来启动celerycam和一个Django Admin网络服务器,在相同的过程中,您可以
    使用它来在一个不错的Web界面中编辑定期任务:

    Also there's the djcelerymon command which can be used for non-Django projects to start celerycam and a Django Admin webserver in the same process, you can use that to also edit your periodic tasks in a nice web interface:

       $ djcelerymon
    

    (请注意,出于某些原因,不能使用Ctrl + C停止djcelerymon,您
    必须使用Ctrl + Z +杀死%1)

    (Note for some reason djcelerymon can't be stopped using Ctrl+C, you have to use Ctrl+Z + kill %1)

    这篇关于如何向Celery动态添加/删除定期任务(celerybeat)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆