Introduction
In today's fast-paced world, it's important for web applications to be able to handle multiple tasks simultaneously without slowing down the user experience. This is where Celery, a distributed task queue, comes in. Celery allows for the execution of tasks in the background, freeing up the main thread to handle other requests. In this tutorial, we will learn how to install and configure Celery workers with Django for background tasks.
Prerequisites
In order to follow this tutorial, you will need the following:
- Basic knowledge of Django
- Python 3.6 or higher
- A virtual environment for your Django project
- A database for your Django project
Step 1: Install Celery
The first step is to install Celery in your virtual environment. Open your terminal and activate your virtual environment. Then, use the following command to install Celery:
pip install celery
Once Celery is installed, we need to install a message broker. Celery supports various message brokers such as RabbitMQ, Redis, and Amazon SQS. For this tutorial, we will be using RabbitMQ. Install RabbitMQ using the following command:
sudo apt-get install rabbitmq-server
Step 2: Configure Celery in Django
Next, we need to configure Celery in our Django project. Open your project's settings.py
file and add the following code:
# Celery ConfigurationCELERY_BROKER_URL = 'amqp://localhost'CELERY_RESULT_BACKEND = 'django-db'CELERY_ACCEPT_CONTENT = ['application/json']CELERY_RESULT_SERIALIZER = 'json'CELERY_TASK_SERIALIZER = 'json'CELERY_TIMEZONE = 'UTC'
Let's break down what each of these configurations means:
CELERY_BROKER_URL
- This is the URL of the message broker we will be using, in this case, RabbitMQ.CELERY_RESULT_BACKEND
- This is the backend where Celery will store the results of the tasks. We will be using the Django database as our backend.CELERY_ACCEPT_CONTENT
- This is the list of content types that Celery will accept. We will be using JSON.CELERY_RESULT_SERIALIZER
- This is the serializer that will be used to store the results of the tasks. We will be using JSON.CELERY_TASK_SERIALIZER
- This is the serializer that will be used to send the tasks to the workers. We will be using JSON.CELERY_TIMEZONE
- This is the timezone that Celery will use to schedule tasks. We will be using UTC.
Step 3: Create a Celery App
Now, we need to create a Celery app in our Django project. This app will contain all the tasks that we want to run in the background. In your project's root directory, create a new file called celery.py
and add the following code:
# celery.pyfrom celery import Celeryfrom django.conf import settingsimport os# set the default Django settings module for the 'celery' program.os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'project.settings')app = Celery('project')# Using a string here means the worker will notUseful Links: