Skip to content

Commit 2463f47

Browse files
committed
Django & Celery - How to Integrate
1 parent de5f0f4 commit 2463f47

File tree

1 file changed

+69
-25
lines changed

1 file changed

+69
-25
lines changed

docs/technologies/django/integrate-celery.mdx

Lines changed: 69 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,33 +10,49 @@ import SubHeading from "@site/src/components/SubHeading";
1010
This page explains `how to integrate` **Django with Celery**, two powerful technologies used in many applications and production-ready projects.
1111
The `source code` is saved on GitHub for later reference and support.
1212

13-
- 👉https://github.com/app-generator/sample-django-tasks-manager - `sample project`
14-
- 👉 Free [Support](https://appseed.us/support/) via Email & Discord
13+
- 👉 [Integrate Django & Celery](https://github.com/app-generator/django-tasks-manager) - `sample project`
14+
- 🚀 Free [Support](https://appseed.us/support/) via Email & Discord
15+
16+
In the end, the integration will be similar to this UI:
17+
18+
![Django & Celery Integration](https://user-images.githubusercontent.com/51070104/234322406-e737605c-51b7-4e9c-b0c6-e7b37515f54d.png)
1519

1620
<br />
1721

1822
## Introduction
19-
Django is a leading web framework and it has a lot of features built into it. Although Django is a fast and scalable web framework, certain tasks can increase the strain on the web servers and also reduce the response time of our Django application. Celery is a package that aims to solve this problem.
2023

21-
### What is Celery?
22-
Celery is a task queue, it is a powerful tool for managing asynchronous tasks in Django. It can help developers to build more scalable and efficient applications by running time-consuming tasks on separate processes.
24+
Django is a leading web framework and it has a lot of features built into it.
25+
Although Django is a fast and scalable web framework, certain tasks can increase the strain on the web servers and also reduce the response time of our Django application. Celery is a package that aims to solve this problem.
26+
27+
### ✅ What is Celery?
2328

24-
Celery works by using a message queue, such as RabbitMQ or Redis(also known as brokers), to distribute tasks across worker processes. When a task is submitted, it is added to the queue, and a worker process executes it. This allows tasks to be executed asynchronously, without blocking the main Django process or affecting the responsiveness of the application.
29+
Celery is a task queue, it is a powerful tool for managing asynchronous tasks in Django.
30+
It can help developers to build more scalable and efficient applications by running time-consuming tasks on separate processes.
2531

26-
Tasks such as sending emails, processing data, or generating reports, can be time-consuming and thus reduce the response time of the Django application. These tasks can be sent to worker processes to be done asynchronously and also speed up our Django application.
32+
Celery works by using a message queue, such as RabbitMQ or Redis(also known as brokers), to distribute tasks across worker processes.
33+
When a task is submitted, it is added to the queue, and a worker process executes it. This allows tasks to be executed asynchronously, without blocking the main Django process or affecting the responsiveness of the application.
34+
35+
Tasks such as sending emails, processing data, or generating reports, can be time-consuming and thus reduce the response time of the Django application.
36+
These tasks can be sent to worker processes to be done asynchronously and also speed up our Django application.
37+
38+
### ✅ Why using Celery?
2739

28-
### Why use Celery?
2940
- Celery increases the responsiveness of the application by handling processes that will normally block the Django process.
3041
- It allows developers to schedule tasks to run at a specific time or interval.
3142
- It provides mechanisms for retrying failed tasks and handling errors.
3243
- It can also store the results of tasks in a backend such as Redis, so they can be retrieved later.
3344

3445
Using Django and Celery will improve the user experience of your application and also reduce the on Django servers. Django and Celery can integrate seamlessly because Django is supported out of the box.
3546

36-
### Django Async Vs. Celery
37-
Django Async, provides a built-in way to handle asynchronous tasks, without relying on third-party libraries like Celery. It uses async/await syntax and the asyncio library to run tasks in the background, inside the same Django process as the web server. This means that there is no need to set up a separate task queue, broker, or worker process, as is the case with Celery.
47+
### ✅ Django Async Vs. Celery
48+
49+
Django Async, provides a built-in way to handle asynchronous tasks, without relying on third-party libraries like Celery.
50+
It uses async/await syntax and the asyncio library to run tasks in the background, inside the same Django process as the web server.
51+
This means that there is no need to set up a separate task queue, broker, or worker process, as is the case with Celery.
3852

39-
Celery, on the other hand, is a mature, battle-tested, and widely-used distributed task queue platform that provides a feature-rich and robust solution for running asynchronous tasks in Django and other Python applications. It requires a separate server and worker processes to execute tasks, usually on a different machine or cluster of machines, which increases its scalability and performance.
53+
Celery, on the other hand, is a mature, battle-tested, and widely-used distributed task queue platform that provides a feature-rich and robust solution for
54+
running asynchronous tasks in Django and other Python applications. It requires a separate server and worker processes to execute tasks, usually on a different machine or cluster of machines,
55+
which increases its scalability and performance.
4056

4157
Some other key differences between Django Async and Celery are:
4258

@@ -46,13 +62,19 @@ Some other key differences between Django Async and Celery are:
4662

4763
In summary, Django Async provides a simple, built-in solution for handling asynchronous tasks, while Celery provides a more robust and scalable solution for large, complex web applications.
4864

49-
## Setting up Development Environment
50-
To use Celery, you need a broker such as RabbitMQ or Redis. There are several other brokers, See [Celery Broker Overview](https://docs.celeryq.dev/en/stable/getting-started/backends-and-brokers/index.html#broker-overview) for a full list. For this tutorial, we will be using Redis.
65+
## Setting up the Environment
5166

52-
### Redis Installation
53-
Redis is an in-memory data structure store, used as a distributed, in-memory key-value database, cache and message broker, with optional durability. Redis supports different kinds of abstract data structures, such as strings, lists, maps, sets, sorted sets, HyperLogLogs, bitmaps, streams, and spatial indices. It can be used as a database, cache, streaming engine, and **message broker**.
67+
To use Celery, you need a broker such as RabbitMQ or Redis. There are several other brokers, s
68+
see [Celery Broker Overview](https://docs.celeryq.dev/en/stable/getting-started/backends-and-brokers/index.html#broker-overview) for a full list. For this tutorial, we will be using Redis.
5469

55-
Redis and Celery are not supported on Windows, Although you can use Windows Subsystem for Linux (WSL) and follow the steps for Linux. Follow this [link](https://learn.microsoft.com/en-us/windows/wsl/install) to set up WSL for your machine and follow the steps for Linux to install Redis on your machine.
70+
### ✅ Redis Installation
71+
72+
Redis is an in-memory data structure store, used as a distributed, in-memory key-value database, cache and message broker, with optional durability.
73+
Redis supports different kinds of abstract data structures, such as strings, lists, maps, sets, sorted sets, HyperLogLogs, bitmaps, streams, and spatial indices.
74+
It can be used as a database, cache, streaming engine, and **message broker**.
75+
76+
Redis and Celery are not supported on Windows, Although you can use Windows Subsystem for Linux (WSL) and follow the steps for Linux.
77+
Follow this [link](https://learn.microsoft.com/en-us/windows/wsl/install) to set up WSL for your machine and follow the steps for Linux to install Redis on your machine.
5678

5779
For `Linux`:
5880

@@ -78,7 +100,8 @@ The command below will start a new Docker container with Redis installed, and li
78100
$ docker run -d -p 6379:6379 redis
79101
```
80102

81-
### Creating a Django project
103+
### ✅ Create a Django project
104+
82105
- First, we will be creating the project directory using the command below
83106
```bash
84107
$ mkdir sample_django_task_manager
@@ -104,7 +127,8 @@ sample_django_task_manager$ source venv/bin/activate
104127
(venv) sample_django_task_manager$ django-admin start project core .
105128
```
106129

107-
### Django - Celery Integration
130+
### ✅ Django & Celery Integration
131+
108132
We will now be making changes to our project to allow it to work with celery.
109133

110134
- Create a file inside the `core` directory called `celery.py` and add the following code
@@ -278,6 +302,7 @@ The `delay` method makes the `slowdown_django_process` function to be executed a
278302
Now we have our application set up to use celery workers.
279303

280304
- Open a new terminal in the project directory and execute the command below
305+
281306
```bash
282307
(venv) sample_django_task_manager$ celery -A core.celery.app worker --loglevel=info
283308
...
@@ -286,16 +311,19 @@ Now we have our application set up to use celery workers.
286311
. django_celery.tasks.slowdown_django_process
287312
...
288313
```
314+
289315
The `-A` option for the `celery` command is the application instance to be used when creating the workers, `core.celery.app` is the app `Celery` instance that was created in `core/celery.py`. `--loglevel=info` helps us get verbose logs on the terminal.
290316

291317
Under `[tasks]` we see all the tasks that have been created being recognized by celery and registered.
292318

293319
- From your browser open `http://127.0.0.1:8000` and see how fast you get the response to your request. If you check the terminal running celery, you will see something like
320+
294321
```bash
295322
...
296323
[2023-04-23 18:45:42,120: INFO/MainProcess] Task django_celery.tasks.slowdown_django_process[952c4afe-c7c0-46a7-9373-b0002f8aefa1] received
297-
[2023-04-23 18:45:52,407: INFO/ForkPoolWorker-4] Task django_celery.tasks.slowdown_django_process[952c4afe-c7c0-46a7-9373-b0002f8aefa1] succeeded in 10.285309484999743s: {'status': 'Process slowdown complete'}
324+
[2023-04-23 18:45:52,407: INFO/ForkPoolWorker-4] Task django_celery.tasks.slowdown_django_process[952c4afe-c7c0-46a7-9373-b0002f8aefa1] succeeded in 10.285309484999743s
298325
```
326+
299327
This shows the task being executed by celery, the time taken to complete the task and the result of the task, which is the return value of the function `slowdown_django_process`.
300328

301329
- Open `http://127.0.0.1:8000/admin` from your browser and click on `Task Results` to see all the tasks executed by celery and other information surrounding the task. Clicking on the `id` of any task will open a new page showing more information concerning that task.
@@ -306,8 +334,12 @@ This shows the task being executed by celery, the time taken to complete the tas
306334

307335
![Workspace 1_016 resized](https://user-images.githubusercontent.com/57325382/233859472-07d63c9a-0bd2-4e33-9b84-711623930be8.png)
308336

309-
## Django Tasks Manager
310-
Django Tasks Manager is a **Django & Celery** integration - This library is actively supported by [AppSeed](https://appseed.us/). It can be used to manage tasks, check tasks' status and also the log and result of tasks performed by celery.
337+
<br />
338+
339+
## [Django Tasks Manager](https://github.com/app-generator/django-tasks-manager)
340+
341+
Django Tasks Manager is a **Django & Celery** integration - This library is actively supported by [AppSeed](https://appseed.us/).
342+
It can be used to manage tasks, check tasks' status and also the log and result of tasks performed by celery.
311343

312344
> Features:
313345
@@ -323,7 +355,8 @@ Django Tasks Manager is a **Django & Celery** integration - This library is acti
323355

324356
The samples are there to provide a usage guide, and you can write your tasks and execute them using `Django Tasks Manager`.
325357

326-
### Setting up Django Tasks Manager
358+
### ✅ Setting up Django Tasks Manager
359+
327360
- `Django Tasks Manager` can be installed using `pip`, install the package using the command below
328361
```bash
329362
(venv) sample_django_task_manager$ pip install django-tasks-manager
@@ -378,9 +411,20 @@ By adding Python files into the `celery_scripts` folder, we can execute several
378411

379412
After adding those Python scripts, we can now start executing Python scripts using `django-tasks-manager`.
380413

414+
<br />
415+
381416
## Conclusion
382-
In conclusion, integrating Django and Celery can enhance the functionality and efficiency of your application by allowing you to offload time-consuming or resource-intensive tasks to background workers. With Celery handling your asynchronous task queue, Django can focus on serving user requests and generating responses.
383417

384-
To successfully integrate Django and Celery, you need to install and configure Celery to work with your Django project. This includes setting up a Celery worker, task queue, broker, and task scheduler. Once the setup is complete, you can create and register tasks in Celery's task queue, and use annotations to define and pass arguments to your tasks.
418+
In conclusion, integrating Django and Celery can enhance the functionality and efficiency of your application by allowing you to offload time-consuming or resource-intensive tasks to background workers.
419+
With Celery handling your asynchronous task queue, Django can focus on serving user requests and generating responses.
420+
421+
To successfully integrate Django and Celery, you need to install and configure Celery to work with your Django project.
422+
This includes setting up a Celery worker, task queue, broker, and task scheduler. Once the setup is complete, you can create and register tasks in Celery's task queue, and use annotations to define and pass arguments to your tasks.
423+
424+
While the integration process can seem complex, it is worth the effort, especially considering the numerous benefits it offers.
425+
With Django and Celery combined, you can build faster, more responsive, and more scalable web applications to meet the needs of your users.
426+
427+
## Resources
385428

386-
While the integration process can seem complex, it is worth the effort, especially considering the numerous benefits it offers. With Django and Celery combined, you can build faster, more responsive, and more scalable web applications to meet the needs of your users.
429+
- 👉 More [Django Starters](https://appseed.us/admin-dashboards/django/) crafted by `AppSeed`
430+
- 🚀 Free [Support](https://appseed.us/support/) via Email & Discord

0 commit comments

Comments
 (0)