Celery helps your Django app do tasks in the background. This means your app stays fast and can handle many jobs without waiting.
Celery installation and setup in Django
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
Django
pip install celery # In your Django project folder, create a celery.py file from celery import Celery import os os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'your_project.settings') app = Celery('your_project') app.config_from_object('django.conf:settings', namespace='CELERY') app.autodiscover_tasks()
Replace your_project with your actual Django project name.
Celery uses a message broker like Redis or RabbitMQ to manage tasks.
Examples
Django
pip install celery redis
myproject.Django
# celery.py in your Django project from celery import Celery import os os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings') app = Celery('myproject') app.config_from_object('django.conf:settings', namespace='CELERY') app.autodiscover_tasks()
Django
# settings.py additions CELERY_BROKER_URL = 'redis://localhost:6379/0'
Sample Program
This shows how to set up Celery, create a simple task to add two numbers, and run it asynchronously.
Django
# celery.py from celery import Celery import os os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings') app = Celery('myproject') app.config_from_object('django.conf:settings', namespace='CELERY') app.autodiscover_tasks() # tasks.py inside any Django app from celery import shared_task @shared_task def add(x, y): return x + y # Usage in Django shell # from myapp.tasks import add # result = add.delay(4, 6) # print(result.get(timeout=10))
Important Notes
Make sure Redis or your chosen broker is running before starting Celery workers.
Run Celery worker with: celery -A your_project worker --loglevel=info
Use delay() to call tasks asynchronously.
Summary
Celery lets Django run tasks in the background to keep the app fast.
Install Celery and a broker like Redis, then configure Celery in your project.
Create tasks with @shared_task and call them with delay().
Practice
1. What is the main purpose of using Celery in a Django project?
easy
Solution
Step 1: Understand Celery's role
Celery is a tool for running tasks asynchronously, meaning it handles tasks in the background.Step 2: Compare options
Creating database models automatically, styling the frontend of the Django app, and replacing Django's built-in ORM are unrelated to Celery; only running time-consuming tasks in the background without blocking the main app correctly describes its purpose.Final Answer:
To run time-consuming tasks in the background without blocking the main app -> Option DQuick Check:
Celery = background task runner [OK]
Hint: Celery runs slow tasks behind scenes to keep app fast [OK]
Common Mistakes:
- Thinking Celery manages database models
- Confusing Celery with frontend styling tools
- Assuming Celery replaces Django ORM
2. Which command correctly installs Celery and Redis as the message broker for a Django project?
easy
Solution
Step 1: Identify the correct packages
Celery requires the 'celery' package and a broker like Redis, installed via 'redis' package.Step 2: Evaluate commands
Only 'pip install celery redis' installs both needed packages correctly; others are incorrect or non-existent.Final Answer:
pip install celery redis -> Option AQuick Check:
Install celery and redis packages = pip install celery redis [OK]
Hint: Use pip install celery redis to get both packages [OK]
Common Mistakes:
- Installing 'django-celery' which is outdated
- Using non-existent package names
- Forgetting to install Redis client
3. Given this Celery task in Django:
@shared_task
def add(x, y):
return x + y
result = add.delay(4, 5)
print(result.get(timeout=10))
What will be printed when this code runs correctly?medium
Solution
Step 1: Understand task execution
The task 'add' runs asynchronously; calling 'delay(4, 5)' queues the task to add 4 and 5.Step 2: Retrieve result with get()
'result.get(timeout=10)' waits for the task to finish and returns the sum 9.Final Answer:
9 -> Option AQuick Check:
add(4,5) = 9 [OK]
Hint: delay queues task; get() fetches result [OK]
Common Mistakes:
- Expecting delay() to return result immediately
- Confusing task object with result value
- Not using get() to wait for task completion
4. You wrote this Celery setup in your Django project:
from celery import Celery
app = Celery('proj')
app.config_from_object('django.conf:settings', namespace='CELERY')
app.autodiscover_tasks()
But your tasks are not running. What is the most likely mistake?medium
Solution
Step 1: Check broker setup
Celery needs a running message broker (Redis or RabbitMQ) to send and receive tasks.Step 2: Analyze other options
Imports and decorators are correct; app.start() is not required in settings.py; missing broker is common cause.Final Answer:
Not running a message broker like Redis or RabbitMQ -> Option BQuick Check:
Broker must run for tasks to work [OK]
Hint: Always start Redis or RabbitMQ before Celery workers [OK]
Common Mistakes:
- Forgetting to start Redis or RabbitMQ server
- Assuming Celery works without a broker
- Misplacing @shared_task decorator
5. You want to configure Celery in your Django project to use Redis as the broker and ensure tasks are auto-discovered. Which of the following is the correct minimal setup in your
celery.py file?hard
Solution
Step 1: Set broker URL directly in Celery instance
from celery import Celery app = Celery('proj', broker='redis://localhost:6379/0') app.autodiscover_tasks() correctly passes Redis broker URL when creating Celery app, which is a minimal working setup.Step 2: Evaluate other options
The first uses an invalid path to settings ('settings' instead of 'django.conf:settings'); the second requires defining CELERY_BROKER_URL in Django settings.py first; the third is correct syntax-wise but setting broker_url attribute after creation is less minimal than passing in constructor.Final Answer:
from celery import Celery app = Celery('proj', broker='redis://localhost:6379/0') app.autodiscover_tasks() -> Option CQuick Check:
Broker URL in constructor = from celery import Celery app = Celery('proj', broker='redis://localhost:6379/0') app.autodiscover_tasks() [OK]
Hint: Pass broker URL directly to Celery constructor for quick setup [OK]
Common Mistakes:
- Setting broker_url attribute instead of passing in constructor
- Misconfiguring config_from_object with wrong settings path
- Omitting broker URL causing connection failure
