Home > Backend Development > Python Tutorial > Use Celery Redis Django to optimize website asynchronous task processing process

Use Celery Redis Django to optimize website asynchronous task processing process

WBOY
Release: 2023-09-27 08:38:01
Original
853 people have browsed it

使用Celery Redis Django优化网站异步任务处理流程

Use Celery Redis Django to optimize the website asynchronous task processing process

Foreword:
When developing a website, we often encounter some time-consuming operations, such as sending emails , generate reports, crawl data, etc. If these operations are synchronous, it will cause the user to freeze while waiting for the operation to complete, worsening the user experience. In order to solve this problem, you can use asynchronous tasks to handle time-consuming operations, and Celery is currently a popular Python asynchronous task processing framework.

Celery is a distributed task queue framework that can put tasks into the queue and execute them asynchronously by the Celery worker node (Worker), thereby achieving asynchronous execution of tasks. In the use of Celery, Redis is a commonly used message broker (Broker), used to deliver Celery tasks.

In order to better demonstrate the use of Celery Redis Django, this article will take the function of sending emails as an example. The following will be divided into the following parts to explain:

1. Environment preparation
2. Installing Celery and Redis
3. Configuring Celery
4. Implementing asynchronous tasks in Django
5. Start Celery Worker
6. Write the test module
7. Run the test and summary

1. Environment preparation
Before starting development, you need to ensure that Python, Django and Redis. If it is not installed, you can install it through pip.

2. Install Celery and Redis
$ pip install celery
$ pip install redis

3. Configure Celery
Add the following in the settings.py file of the Django project Configuration:

Redis as message broker

BROKER_URL = 'redis://localhost:6379/0'

Store task results locally

CELERY_RESULT_BACKEND = 'redis://localhost:6379/0'

Define task module

CELERY_IMPORTS = (

'your_app.tasks',
Copy after login
Copy after login

)

Set the default task queue name

CELERY_DEFAULT_QUEUE = 'default'

4. Implement asynchronous tasks in Django
Create a tasks.py file in some apps of the Django project to store the functions of asynchronous tasks. For example, we create an emails app, and then write an asynchronous task for sending emails in emails/tasks.py.

emails/tasks.py

from celery import task
from django.core.mail import send_mail

@task()
def send_email_task(subject, message, from_email, recipient_list):

send_mail(subject, message, from_email, recipient_list)
Copy after login
Copy after login

It should be noted that the task decorator here is provided by Celery and is used to register the function as a Celery task.

5. Start Celery Worker
Open the terminal, switch to the root directory of the Django project, and execute the following command to start Celery Worker.

$ celery -A project_name worker -l info

Where, project_name is the name of the Django project.

6. Write a test module
Write a view function for sending emails in views.py of an app in the Django project, and bind the view function to a certain URL.

views.py

from django.shortcuts import render
from .tasks import send_email_task

def send_email_view(request):

if request.method == 'POST':
    subject = request.POST.get('subject')
    message = request.POST.get('message')
    from_email = request.POST.get('from_email')
    recipient_list = [request.POST.get('recipient_email')]
    
    # 调用异步任务
    send_email_task.delay(subject, message, from_email, recipient_list)
    
    return render(request, 'success.html')

return render(request, 'send_email.html')
Copy after login

7. Run test and summary
Start the Django project, then access the URL address corresponding to the send_email_view view through the browser, fill in the relevant information, and click the send email button.

It can be observed that the email will be sent asynchronously in the background, and the interface will respond immediately with a prompt message, and the user does not need to wait for the email to be sent. This is the asynchronous task processing process implemented using Celery Redis Django.

Summary:
Using Celery Redis Django can easily implement asynchronous task processing on the website. By putting time-consuming operations into Celery asynchronous tasks, you can effectively improve website performance and user experience. In the specific use process, we need to install and configure Celery and define the task module, and then call the asynchronous task function in Django to achieve asynchronous processing. Different tasks can be processed concurrently through different workers, improving the concurrent processing capabilities of the entire system.
Specific code example:

settings.py

Redis as message broker

BROKER_URL = 'redis://localhost:6379/0'

Storing task results locally

CELERY_RESULT_BACKEND = 'redis://localhost:6379/0'

Define task module

CELERY_IMPORTS = (

'your_app.tasks',
Copy after login
Copy after login

)

Set the default task queue name

CELERY_DEFAULT_QUEUE = 'default'

tasks.py

from celery import task
from django.core. mail import send_mail

@task()
def send_email_task(subject, message, from_email, recipient_list):

send_mail(subject, message, from_email, recipient_list)
Copy after login
Copy after login

views.py

from django.shortcuts import render
from .tasks import send_email_task

def send_email_view(request):

if request.method == 'POST':
    subject = request.POST.get('subject')
    message = request.POST.get('message')
    from_email = request.POST.get('from_email')
    recipient_list = [request.POST.get('recipient_email')]
    
    # 调用异步任务
    send_email_task.delay(subject, message, from_email, recipient_list)
    
    return render(request, 'success.html')

return render(request, 'send_email.html')
Copy after login

The above is the detailed content of Use Celery Redis Django to optimize website asynchronous task processing process. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template