简体   繁体   English

在应用程序中的芹菜(Django)中添加定期任务

[英]Adding periodic tasks in celery (django) inside an app

In celery 4> the periodic_task decorator is deprecated. 在celery 4>中, periodic_task装饰器已弃用。 The recommendation now is to define every periodic task inside where you initialize celery (based on what I can find, and the docs ). 现在的建议是定义初始化celery的内部每个定期任务(根据我能找到的内容和docs )。

In my django setup, I have a lot of small apps, and having a central place to setup period tasks won't do it. 在django设置中,我有很多小型应用程序,而在设置周期任务的中心位置却无法做到。

I am still learning my way around celery, but I think my solution is "good". 我仍然在学习芹菜的方法,但是我认为我的解决方案是“好的”。 Is this the wrong way to solve this, or is there another way now that periodic_task is gone? 这是解决此问题的错误方法,还是periodic_task消失了还有另一种方法?

from celery.schedules import crontab
from lib.celery_instance import app  # The celery instance

@app.task()
def mytask():
    # do something...
    return 123

app.add_periodic_task(crontab(hour=8, minute=45), mytask.s())

Another approach to centrally manage the periodic tasks could be to define a dict in the settings. 集中管理定期任务的另一种方法可以是在设置中定义命令。

CELERY_BEAT_SCHEDULE = {
    'some name': {
        'task': 'myapp.tasks.do_something',
        'schedule':  crontab(hour=0, minute=0),
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM