django celery
Install Celery and Redis
Celery requires a message broker to send and receive messages. Redis is a common choice. Install Celery and Redis using pip:
Example Scenarios for Using Celery
Example
Example 1: Sending an Email Notification
When a user signs up on your platform, you might want to send them a welcome email. Sending the email can take some time, especially if the email server is slow or the network is congested. Using Celery, you can offload the email sending process to a background task:
from celery import shared_task
from django.core.mail import send_mail
@shared_task
def send_welcome_email(user_id):
from django.contrib.auth.models import User
user = User.objects.get(id=user_id)
send_mail(
'Welcome!',
'Thank you for signing up, {}'.format(user.username),
'from@example.com',
[user.email],
fail_silently=False,
)
from django.shortcuts import render, redirect
from django.contrib.auth.models import User
from .tasks import send_welcome_email
def signup(request):
if request.method == 'POST':
username = request.POST['username']
email = request.POST['email']
password = request.POST['password']
user = User.objects.create_user(username, email, password)
send_welcome_email.delay(user.id)
return redirect('home')
return render(request, 'signup.html')
Example 2: Generating a Report
Generating a detailed report based on user data can be time-consuming. Using Celery, you can generate the report in the background and notify the user when it's ready:
```python "tasks.py" from celery import shared_task import time
@shared_task def generate_report(user_id): # Simulate a long-running task time.sleep(10) return 'Report for user {}'.format(user_id)
```python title="views.py"
from django.shortcuts import render
from django.http import JsonResponse
from .tasks import generate_report
def request_report(request):
if request.method == 'POST':
user_id = request.user.id
result = generate_report.delay(user_id)
return JsonResponse({'task_id': result.id})
return render(request, 'request_report.html')
Celery provides several tools for monitoring and managing tasks:
-
Flower: A real-time web-based monitoring tool for Celery.
- Install Flower:
pip install flower
- Run Flower:
celery -A myproject flower
- Access Flower: Navigate to
http://localhost:5555/
in your web browser.
- Install Flower:
-
Celery Events: Built-in command-line tool for monitoring.
- Run Celery Events: celery -A myproject events
Summary
Celery is an essential tool for handling asynchronous tasks in Django applications. It is particularly useful for long-running tasks, I/O bound tasks, scheduled tasks, real-time updates, resource-intensive tasks, integration with external APIs, and tasks that require retry mechanisms. By offloading such tasks to Celery, you can improve the responsiveness, scalability, and maintainability of your Django applications.