Examples of Django and Celery: Periodic Tasks

僤鯓⒐⒋嵵緔 提交于 2019-12-02 17:34:07
arie

What's wrong with the example from the docs?

from celery.task import PeriodicTask
from clickmuncher.messaging import process_clicks
from datetime import timedelta


class ProcessClicksTask(PeriodicTask):
    run_every = timedelta(minutes=30)

    def run(self, **kwargs):
        process_clicks()

You could write the same task using a decorator:

from celery.task.schedules import crontab
from celery.task import periodic_task

@periodic_task(run_every=crontab(minute="*/30"))
def process_clicks():
    ....

The decorator syntax simply allows you to turn an existing function/task into a periodic task without modifying them directly.

For the tasks to be executed celerybeat must be running.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!