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
Recall & Review
beginner
What is a task in Django when using Celery?
A task is a Python function that runs asynchronously in the background, outside the normal request-response cycle, to perform time-consuming or scheduled work.
Click to reveal answer
beginner
How do you define a simple Celery task in Django?
You define a Celery task by creating a Python function and decorating it with @app.task where app is your Celery application instance.
Click to reveal answer
intermediate
Why should tasks be idempotent in Django Celery?
Tasks should be idempotent so that running them multiple times does not cause unintended side effects, ensuring reliability if a task is retried or duplicated.
Click to reveal answer
beginner
What is the purpose of the delay() method when calling a Celery task?
The delay() method sends the task to the Celery worker to run asynchronously, instead of running it immediately in the current process.
Click to reveal answer
beginner
Where do you usually place task definitions in a Django project?
Tasks are usually placed in a tasks.py file inside a Django app folder to keep task code organized and close to related app logic.
Click to reveal answer
Which decorator is used to define a Celery task in Django?
A@django.task
B@task.run
C@celery.run
D@app.task
✗ Incorrect
The correct decorator to define a Celery task is @app.task, where app is your Celery application instance.
What does calling my_task.delay() do?
ARuns the task immediately in the current process
BSchedules the task to run asynchronously by a worker
CDeletes the task from the queue
DConverts the task to a synchronous function
✗ Incorrect
Calling delay() sends the task to a Celery worker to run asynchronously in the background.
Why is it important for tasks to be idempotent?
ATo improve task speed
BTo reduce memory usage
CTo avoid side effects if retried or run multiple times
DTo make tasks synchronous
✗ Incorrect
Idempotency ensures tasks can be safely retried or duplicated without causing errors or unwanted changes.
Where should you place task definitions in a Django app?
AIn <code>tasks.py</code>
BIn <code>models.py</code>
CIn <code>views.py</code>
DIn <code>settings.py</code>
✗ Incorrect
Tasks are best organized in a tasks.py file inside the Django app folder.
What is the main benefit of defining tasks in Django with Celery?
ATo make the website load faster by running code asynchronously
BTo simplify database queries
CTo handle user authentication
DTo style the website
✗ Incorrect
Defining tasks lets you run slow or heavy work in the background, so the website stays fast and responsive.
Explain how to define and call a simple Celery task in a Django project.
Think about how you tell Celery which function is a task and how you ask it to run later.
You got /3 concepts.
Why should tasks be idempotent and where do you place them in a Django app?
Consider reliability and project organization.
You got /2 concepts.
Practice
(1/5)
1. What decorator is commonly used to define a background task in Django with Celery?
easy
A. @task_runner
B. @shared_task
C. @async_task
D. @background_task
Solution
Step 1: Understand task definition in Django with Celery
Celery uses the @shared_task decorator to mark functions as tasks that can run asynchronously.
Step 2: Identify the correct decorator
Among the options, only @shared_task is the correct and standard decorator for defining tasks.
Final Answer:
@shared_task -> Option B
Quick Check:
Task decorator = @shared_task [OK]
Hint: Remember: Celery tasks use @shared_task decorator [OK]
Common Mistakes:
Using @background_task which is not a Celery decorator
Confusing @async_task with async/await syntax
Using @task_runner which is not valid in Django
2. Which of the following is the correct way to call a Celery task asynchronously in Django?
easy
A. my_task.run()
B. my_task.execute()
C. my_task.delay()
D. my_task.start()
Solution
Step 1: Recall how to call Celery tasks asynchronously
Celery tasks are called asynchronously using the delay() method on the task function.
Step 2: Identify the correct method
Only delay() triggers the task asynchronously; other methods like run() execute synchronously or do not exist.
Final Answer:
my_task.delay() -> Option C
Quick Check:
Async call method = delay() [OK]
Hint: Use delay() to run tasks asynchronously [OK]
Common Mistakes:
Calling run() which runs task synchronously
Using execute() which is not a Celery method
Trying start() which does not exist for tasks
3. Given this task definition:
from celery import shared_task
@shared_task
def add(x, y):
return x + y
result = add.delay(4, 5)
What will result.get() return?
medium
A. 9
B. None
C. An AsyncResult object
D. A syntax error
Solution
Step 1: Understand the task and its call
The add function adds two numbers. Calling add.delay(4, 5) runs it asynchronously and returns an AsyncResult.
Step 2: Using result.get() retrieves the task result
Calling result.get() waits for the task to finish and returns the sum, which is 9.
Final Answer:
9 -> Option A
Quick Check:
Task result = 9 [OK]
Hint: delay() returns AsyncResult; get() fetches the actual result [OK]
Common Mistakes:
Thinking delay() returns the result immediately
Confusing AsyncResult object with the actual result
Expecting None because task runs asynchronously
4. Identify the error in this task definition:
from celery import shared_task
@shared_task
def multiply(x, y):
return x * y
medium
A. Function name is invalid
B. Missing @shared_task decorator
C. Using delay() incorrectly
D. Indentation error in function body
Solution
Step 1: Check the function syntax
The function body must be indented inside the function definition. Here, return x * y is not indented.
Step 2: Identify the error type
Python requires indentation for blocks. Missing indentation causes an IndentationError.
Final Answer:
Indentation error in function body -> Option D
Quick Check:
Python blocks need indentation [OK]
Hint: Check indentation inside function definitions [OK]
Common Mistakes:
Ignoring indentation errors
Assuming decorator is missing when it is present
Confusing function name validity with syntax errors
5. You want to define a task that sends emails but only if the email address is not empty. Which of these task definitions correctly applies this condition?
hard
A. from celery import shared_task
@shared_task
def send_email(email):
if email:
# send email code
return 'Sent'
return 'No email provided'
B. from celery import shared_task
@shared_task
def send_email(email):
if email == None:
return 'No email provided'
# send email code
return 'Sent'
C. from celery import shared_task
@shared_task
def send_email(email):
if not email:
return 'Sent'
# send email code
return 'No email provided'
D. from celery import shared_task
def send_email(email):
if email:
# send email code
return 'Sent'
return 'No email provided'
Solution
Step 1: Check for correct task decorator and condition
from celery import shared_task
@shared_task
def send_email(email):
if email:
# send email code
return 'Sent'
return 'No email provided' uses @shared_task and checks if email: which correctly tests for a non-empty email.
Step 2: Verify logic correctness
from celery import shared_task
@shared_task
def send_email(email):
if email:
# send email code
return 'Sent'
return 'No email provided' returns 'Sent' only if email is truthy (not empty), else returns 'No email provided'. This matches the requirement.
Final Answer:
Option A correctly defines the task with the condition -> Option A
Quick Check:
Use if email: to check non-empty string [OK]
Hint: Use if email: to check non-empty strings in tasks [OK]