Examples of Django and Celery: Periodic Tasks
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.
Jonathan May
I make stuff happen. I'm an entrepreneur, blogger, advisor and technology consultant with a strong interest in open source hardware and open business models. I've set up numerous companies across areas as diverse as musical accessories, table football, and crowd-funding. I'm on here to develop many of my technical skills - which have become rusty since leaving university.
Updated on June 14, 2022Comments
-
Jonathan May about 2 years
I have been fighting the Django/Celery documentation for a while now and need some help.
I would like to be able to run Periodic Tasks using django-celery. I have seen around the internet (and the documentation) several different formats and schemas for how one should go about achieving this using Celery...
Can someone help with a basic, functioning example of the creation, registration and execution of a django-celery periodic task? In particular, I want to know whether I should write a task that extends the PeriodicTask class and register that, or whether I should use the @periodic_task decorator, or whether I should use the @task decorator and then set up a schedule for the task's execution.
I don't mind if all three ways are possible, but I would like to see an example of at least one way that works. Really appreciate your help.