Home > Article > Backend Development > Practical experience: Integrating Celery and Redis in Django to implement asynchronous tasks
Practical experience: Integrating Celery and Redis in Django to implement asynchronous tasks
Introduction:
As the complexity of web applications continues to increase, many operations require Spend a lot of time and resources. In order to improve user experience and system efficiency, developers often need to convert some time-consuming operations into asynchronous tasks for execution. In Django, we can implement asynchronous tasks by integrating Celery and Redis. This article will introduce you to how to integrate Celery and Redis in Django, with practical code examples.
pip install celery
Then, to install Redis, you can use the following command:
sudo apt-get install redis-server
After the installation is complete, we need to configure the Django project, Let it know that we will be using Celery and Redis. In the project's settings.py file, add the following code:
# CELERY SETTINGS CELERY_BROKER_URL = 'redis://localhost:6379/0' CELERY_RESULT_BACKEND = 'redis://localhost:6379/0'
Here we specify the local address and port of Redis. Make sure your Redis is running and listening on the specified port.
from celery import Celery app = Celery('myapp', broker='redis://localhost:6379/0') @app.task def add(x, y): return x + y
In this example, we create a Celery application and define a task named add. The add task receives two parameters x and y and returns their sum.
from myapp.tasks import add def my_view(request): x = 10 y = 20 add.delay(x, y) return HttpResponse("Task added to the queue.")
In this example, we imported the add task defined previously and called it in the view function. We use the delay() method to add the task to the Celery queue and immediately return the HttpResponse to the user. This way, users won't be blocked while performing time-consuming operations.
celery -A myapp worker -l info
This will start a Celery worker and start processing the tasks in the queue. You can set the log level with the -l parameter.
pip install flower
After the installation is complete, open a new terminal window and run the following command:
flower -A myapp --port=5555
This will start the Flower server and listen Port 5555. You can visit localhost:5555 in your browser to view information such as currently running tasks and task status.
Conclusion:
By integrating Celery and Redis, we can achieve efficient asynchronous task processing in Django. This article covers the basic steps for configuring and using Celery in a Django project, and provides practical code examples. I hope this article can help you achieve more efficient asynchronous task processing in development.
The above is the detailed content of Practical experience: Integrating Celery and Redis in Django to implement asynchronous tasks. For more information, please follow other related articles on the PHP Chinese website!