Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Periodic tasks with Celery Beat
📖 Scenario: You are building a Django app that needs to perform a task regularly, like sending reminder emails every minute. To do this, you will use Celery with Celery Beat to schedule periodic tasks.
🎯 Goal: Create a Django project setup with Celery and Celery Beat. Define a simple periodic task that runs every minute and prints a message to the console.
📋 What You'll Learn
Create a Celery app instance in a file named celery.py inside your Django project directory.
Add a periodic task schedule in Celery Beat to run every minute.
Define a simple Celery task function called print_hello that prints 'Hello from Celery Beat!' to the console.
Configure Celery Beat in Django settings to enable periodic task scheduling.
💡 Why This Matters
🌍 Real World
Many web applications need to perform background jobs regularly, such as sending emails, cleaning up data, or updating caches. Celery Beat helps schedule these tasks easily.
💼 Career
Understanding how to set up periodic tasks with Celery Beat is valuable for backend developers working with Django to automate routine jobs efficiently.
Progress0 / 4 steps
1
Create Celery app instance
Create a file named celery.py in your Django project directory. Inside it, write code to create a Celery app instance called app with the project name 'myproject'. Import Django settings and configure Celery to use them.
Django
Hint
Use Celery('myproject') to create the app. Set the environment variable for Django settings before creating the app.
2
Add Celery Beat schedule configuration
In your Django settings file, add a dictionary called CELERY_BEAT_SCHEDULE. Inside it, add a task named 'print-hello-every-minute' that runs the task 'myapp.tasks.print_hello' every 60 seconds.
Django
Hint
Define CELERY_BEAT_SCHEDULE as a dictionary with the task name as key and a dictionary with task and schedule keys.
3
Define the periodic task function
In your Django app folder named myapp, create or edit the tasks.py file. Define a Celery task function called print_hello that prints the string 'Hello from Celery Beat!'. Use the @app.task decorator imported from the Celery app instance.
Django
Hint
Import app from your Celery instance and decorate the function with @app.task.
4
Enable Celery Beat in Django settings
In your Django settings file, add django_celery_beat to the INSTALLED_APPS list to enable Celery Beat integration.
Django
Hint
Add 'django_celery_beat' exactly to the INSTALLED_APPS list in settings.py.
Practice
(1/5)
1. What is the main purpose of Celery Beat in a Django project?
easy
A. To schedule and run periodic tasks automatically
B. To handle HTTP requests asynchronously
C. To manage database migrations
D. To serve static files efficiently
Solution
Step 1: Understand Celery Beat's role
Celery Beat is designed to schedule tasks to run at specific times or intervals automatically.
Step 2: Differentiate from other components
Handling HTTP requests, managing migrations, or serving static files are not functions of Celery Beat.
Final Answer:
To schedule and run periodic tasks automatically -> Option A
Quick Check:
Celery Beat = periodic task scheduler [OK]
Hint: Celery Beat schedules tasks, not handles requests [OK]
Common Mistakes:
Confusing Celery Beat with Django's request handling
Thinking Celery Beat manages database or static files
Assuming Celery Beat runs tasks immediately without schedule
2. Which of the following is the correct way to define a periodic task schedule using Celery Beat with a crontab in Django settings?