Celery provides asynchronous job queues, which allows you to run Python functions in the background. Celery is on the Python Package Index (PyPi), and can be easily installed with pip or easy_install and its dependencies.
Installation:$ pip install django-celery && pip install redis
Celery uses a broker to pass messages between your application and Celery worker processes. We are having like rabbitmq, redis but we are using redis as a broker in this article. To initiate a task, an application will adds a message to the queue, which the broker then delivers to a worker.
So we can install redis using the following command:;
sudo apt-get install redis-server
Setting up celery in your project:
1. We need to add 'djcelery' into your project Installed Apps.
INSTALLED_APPS += ( 'djcelery', )
2. To configure celery, we are using redis broker which is easy to install, lightweight, fast:
import djcelery djcelery.setup_loader() BROKER_URL = 'redis://localhost:6379/1'
3. We should specify imports i.e from where tasks are being loaded using CELERY_IMPORTS command in settings.py file
CELERY_IMPORTS = ("testapp.tasks")
4. Celery Periodic Task means which runs at a regular intervals of time. These periodic tasks are scheduled by a celery beat which will executed by a worker.
timedelta(seconds=30) timedelta(minutes=30), timedelta(minutes=*/5) --> which will execute at every 5 minutes
crontab(0, 0, day_of_month='23') --> which will execute on the 23rd day of the every month. crontab(minute='*/5') --> which will execute at every 5 minutes crontab(hour='*', minute='5', day_of_week='mon') --> which will execute for every 5 minutes on mon
5. We can setup celery periodic tasks settings using celery beat schedular.
from celery.schedules import crontab CELERY_TIMEZONE = "Asia/Calcutta" CELERYBEAT_SCHEDULER = "djcelery.schedulers.DatabaseScheduler" CELERYBEAT_SCHEDULE = { # Executes for every 5 minutes on mon,tue,wed,thu,fri,sat 'add-every-day-evening': { 'task': 'testapp.tasks.load_messages_into_database', 'schedule': crontab(hour='*', minute='5', day_of_week='mon,tue,wed,thu,fri,sat'), }, }
You need to ensure that scheduling tasks run with a single schedular only, otherwise it leads to duplicate tasks which runs by multiple schedulers at the same time.
Micropyramid is a software development and cloud consulting partner for enterprise businesses across the world. We work on python, Django, Salesforce, Angular, Reactjs, React Native, MySQL, PostgreSQL, Docker, Linux, Ansible, git, amazon web services. We are Amazon and salesforce consulting partner with 5 years of cloud architect experience. We develop e-commerce, retail, banking, machine learning, CMS, CRM web and mobile applications.
Django-CRM :Customer relationship management based on Django
Django-blog-it : django blog with complete customization and ready to use with one click installer Edit
Django-webpacker : A django compressor tool
Django-MFA : Multi Factor Authentication
Docker-box : Web Interface to manage full blown docker containers and images
More...