简体   繁体   中英

Adding periodic tasks in celery (django) inside an app

In celery 4> the periodic_task decorator is deprecated. The recommendation now is to define every periodic task inside where you initialize celery (based on what I can find, and the 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.

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?

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),
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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