Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Creating Asynchronous Tasks With Celery and Django (Summary)

After you’ve integrated Celery and refactored your Django code, sending feedback in your app is such a great experience that you don’t want to stop sending positive feedback messages!

Handling long-running or compute-expensive tasks asynchronously in the background with Celery, instead of bogging down your web app with tasks that it wasn’t intended to handle, can breathe fresh air into a slow-running application.

Celery aims to provide a quick interface for sending messages to its distributed task queue. In this example, you experienced how little you might need to change to use Celery in your Django app.

In this video course, you learned how to:

  • Recognize effective use cases for Celery
  • Differentiate between Celery beat and Celery workers
  • Integrate Celery and Redis in a Django project
  • Set up asynchronous tasks that run independently of your Django app
  • Refactor Django code to run a task with Celery instead

Keep identifying any tasks that Django doesn’t need to handle. Then offload them to your favorite distributed task queue instead.

Knowing that you can handle sluggish tasks in the background without impairing your user experience also opens up doors to new ideas:

Download

Sample Code (.zip)

24.2 KB
Download

Course Slides (.pdf)

6.1 MB
Avatar image for danilolimadutra

danilolimadutra on March 24, 2024

For anyone who want to run this project in docker, follow the files.

  1. Unzip the course sample code.
  2. Rename, rename the Django root folder for app.
  3. Create Dockerfile.
  4. Create docker-compose.yaml.
  5. Create requirements.txt in the app folder.
  6. Craete the image and the containers
# docker compose commands
$ docker compose build
$ docker compose up

Dockerfile

FROM python:3.11-slim
COPY app /app
WORKDIR /app

ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

RUN pip install -r requirements.txt
RUN python manage.py migrate

docker-compose.yaml

version: '3.8'
services:
  django:
    build: .
    container_name: django
    command: python manage.py runserver 0.0.0.0:8000
    ports:
      - 8000:8000
    volumes:
      - ./app:/app
    environment:
      - DEBUG=1
      - DJANGO_ALLOWED_HOSTS=localhost 127.0.0.1
      - CELERY_BROKER=redis://redis:6379/0
      - CELERY_BACKEND=redis://redis:6379/0
    depends_on:
      - redis
  redis:
    image: "redis:alpine"
    ports:
      - 6379:6379
  celery:
    build: .
    command: celery -A django_celery worker -l INFO
    volumes:
      - ./app:/app
    depends_on:
      - django
      - redis

requirements.txt

celery==5.3.6
Django==5.0.1
redis==5.0.1

Become a Member to join the conversation.