Here is guide to setup Django, Celery, RabbitMQ in Windows and running periodic task.
1. Install RabbitMQ
Go to http://www.rabbitmq.com/download.html, install and check if the services already running
2. Setup celery in your Django Project
Install django-celery modules
1 | pip install django-celery |
3. Example configuration for periodic task
In this example, i create tasks.py in app “schedules”:
1 2 3 4 5 6 7 8 9 10 11 12 | from celery import Celery app = Celery() @app.task() # Celery will ignore results and save memory def update_student(): """ Generate Student data every minutes """ updating_student_database() print("this executed!") |
In Project/project (where settings.py located), create celery_configuration.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | from __future__ import absolute_import import os from celery import Celery from django.conf import settings # set the default Django settings module for the ‘celery’ program. os.environ.setdefault(‘DJANGO_SETTINGS_MODULE’, ‘myproject.settings’) app = Celery(‘myproject’) # Using a string here means the worker will not have to # pickle the object when using Windows. app.config_from_object(‘django.conf:settings’) app.autodiscover_tasks(lambda: settings.INSTALLED_APPS) app.conf.update( CELERY_RESULT_BACKEND=’djcelery.backends.database:DatabaseBackend’, CELERY_TASK_RESULT_EXPIRES=3600, ) if __name__ == ‘__main__’: app.start() |
Then, last time in settings.py:
1 2 3 4 5 6 7 | # Define which task that will executed in periodic task CELERYBEAT_SCHEDULE = { ‘every-minute’: { ‘task’: ‘schedules.tasks.update_student’, ‘schedule’: timedelta(seconds=10), }, } |
Don’t forget to update your __init__.py (located in same folder with settings.py)
1 | from myproject.celery_configuration import app as celery_app |
Then, we just need to run “celeryd” and “celerybeat” to make periodic task working
1 2 | python manage.py celery beat –app=myproject python manage.py celeryd -l INFO |