28

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.

jro
  • 8,540
  • 27
  • 33
Jonathan May
  • 421
  • 1
  • 5
  • 9

1 Answers1

40

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.

ken
  • 1,003
  • 8
  • 14
arie
  • 17,407
  • 5
  • 65
  • 75
  • Thanks for your answer. It's good to know what exactly the decorator is for and why two forms of the same thing exist. Is it correct that I do not have to register PeriodicTasks then? I found this example hard to find in the documentation and it could do with simplification (as you have done above). Thanks again. – Jonathan May Nov 22 '11 at 15:46
  • Hey, here is another example from the docs: http://ask.github.com/celery/reference/celery.decorators.html You don't have to explicitly register the task if you use the decorator. It's pretty similar to the options you have registering your templatetags and filters in Django (https://docs.djangoproject.com/en/dev/howto/custom-template-tags/#registering-the-tag), if you're more familiar with that. – arie Nov 22 '11 at 16:22
  • Thank you again for your help. – Jonathan May Nov 22 '11 at 16:31
  • How do you check that celery has picked up the scheduled tasks? I can see that the task is registered when I run `manage.py celery inspect registered`, but it is not in `manage.py celery inspect scheduled`. I have workers, and beat is running. – hgcrpd Jun 10 '13 at 06:32
  • 3
    The `@periodic_task` decorator is deprecated as of Celery 3.1. https://github.com/celery/celery/issues/1764 – StockB Oct 27 '15 at 13:18