Skip to content

Instantly share code, notes, and snippets.

@adrianp
Created March 10, 2011 18:58
Show Gist options
  • Save adrianp/864665 to your computer and use it in GitHub Desktop.
Save adrianp/864665 to your computer and use it in GitHub Desktop.
Installing django-celery and adding scheduled tasks to a Django project
THESE INSTRUCTIONS ARE FOR OLD VERSIONS OF DJANGO/CELERY/PYTHON!
1. Download the latest version of django-celery from here:
https://github.com/ask/django-celery/archives/master
2. Unzip, go to folder
3. python setupy.py build
4. python setup.py install
5. Download the latest version of django-kombu from here:
http://pypi.python.org/pypi/django-kombu
6. Same install routine as for django-celery
7. Next, do the following changes to your project's settings.py:
a) Add the following line in the head: import djcelery
b) Add the following lines:
djcelery.setup_loader()
CELERYBEAT_SCHEDULER = 'djcelery.schedulers.DatabaseScheduler'
CARROT_BACKEND = "django"
c) Add 'djkombu' and 'djcelery' to INSTALLED_APPS
8. python manage.py syncdb
9. Start your Django application
10. Also run (the Celery daemon):
./manage.py celeryd -v 2 -B -s celery -E -l INFO (at another prompt)
11. Register a task as follows:
from celery.task import task
@task
def Test():
print "This is a task"
return True
12. Go to the admin area of your Django application.
Add a new task via Djcelery > Periodic Tasks:
enter a name, choose the defined tasks in the "Tasks (registered)" list
and choose an Interval (you'll have to create one the first time).
13. Save and go watch the prompt where the Celery daemon is running;
you should see the defined task being fired based on the interval
you selected in the previous step.
Let me know if I forgot anything :)
Troubleshooting:
1. You get an error like this every time a task is fired:
Unknown task ignored: [...]
NotRegistered: u'MyProject.MyApp.Task'
Solution:
You have the problem described here:
http://celeryproject.org/docs/userguide/tasks.html#automatic-naming-and-relative-imports
Mainly, if in settings.py in INSTALLED_APPS you have something like 'MyProject.MyApp'
your tasks won't work. To fix it, simply replace the mentioned application reference with 'MyApp'.
@baranbartu
Copy link

i've followed your instructions all but i've got an error like 'PeriodicTasks' object has no attribute 'id' while i am adding to periodic task. What is the solution?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment