Bird
Raised Fist0
Djangoframework~20 mins

Calling tasks asynchronously in Django - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

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
Challenge - 5 Problems
🎖️
Async Task Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this Django Celery task call?
Consider this Django Celery task defined in tasks.py. What will be printed when the task is called asynchronously?
Django
from celery import shared_task

@shared_task
def add(x, y):
    return x + y

result = add.delay(4, 5)
print(result.ready())
AFalse
BTrue
C9
DNone
Attempts:
2 left
💡 Hint
Think about what delay() returns and when the task finishes.
state_output
intermediate
2:00remaining
What is the value of result.get() after task completion?
Given this Celery task and its asynchronous call, what will result.get() return after the task finishes?
Django
from celery import shared_task

@shared_task
def multiply(x, y):
    return x * y

result = multiply.delay(3, 7)
output = result.get(timeout=10)
print(output)
A10
BNone
CTimeoutError
D21
Attempts:
2 left
💡 Hint
The get() method waits for the task result.
📝 Syntax
advanced
2:30remaining
Which option correctly schedules a periodic task in Django Celery?
You want to schedule a task to run every 10 minutes using Django Celery. Which code snippet correctly sets this up in celery.py or tasks.py?
A
from celery.schedules import crontab
app.conf.beat_schedule = {'task-every-10-minutes': {'task': 'myapp.tasks.my_task', 'schedule': crontab(minute='*/10')}}
Bapp.conf.beat_schedule = {'task-every-10-minutes': {'task': 'myapp.tasks.my_task', 'schedule': 10}}
C
from celery.schedules import schedule
app.conf.beat_schedule = {'task-every-10-minutes': {'task': 'myapp.tasks.my_task', 'schedule': schedule(10)}}
Dapp.conf.beat_schedule = {'task-every-10-minutes': {'task': 'myapp.tasks.my_task', 'interval': 600}}
Attempts:
2 left
💡 Hint
Look for the correct way to specify a periodic schedule using crontab.
🔧 Debug
advanced
2:00remaining
Why does this asynchronous task call raise an error?
Examine the code below. Why does calling send_email.delay() raise an error?
Django
from celery import shared_task

@shared_task
def send_email(to_address):
    print(f"Sending email to {to_address}")

send_email.delay()
ANo error, prints 'Sending email to None'
BSyntaxError due to missing colon in function definition
CTypeError because delay() is called without required argument
DAttributeError because send_email is not a task
Attempts:
2 left
💡 Hint
Check the function parameters and how delay() is called.
🧠 Conceptual
expert
2:30remaining
Which statement about Django Celery task execution is true?
Select the correct statement about how Django Celery executes asynchronous tasks.
ATasks run synchronously in the main Django process by default
BTasks are sent to a message broker and executed by separate worker processes asynchronously
CCalling <code>delay()</code> immediately executes the task in the worker process
DDjango automatically retries failed tasks without any configuration
Attempts:
2 left
💡 Hint
Think about how Celery separates task execution from the Django app.

Practice

(1/5)
1. What is the main purpose of calling tasks asynchronously in a Django app using Celery?
easy
A. To avoid using any external libraries
B. To make the app run slower by adding extra steps
C. To run time-consuming tasks in the background without blocking the main app
D. To run tasks only when the server restarts

Solution

  1. Step 1: Understand asynchronous task calling

    Calling tasks asynchronously means running them in the background so the main app can continue working without waiting.
  2. Step 2: Identify the benefit in Django apps

    This helps keep the app fast and responsive by not blocking user requests with long tasks.
  3. Final Answer:

    To run time-consuming tasks in the background without blocking the main app -> Option C
  4. Quick Check:

    Async tasks = background work = To run time-consuming tasks in the background without blocking the main app [OK]
Hint: Async tasks run in background, keeping app responsive [OK]
Common Mistakes:
  • Thinking async tasks slow down the app
  • Believing async tasks run only on server restart
  • Confusing async with avoiding external libraries
2. Which of the following is the correct way to call a Celery task named send_email asynchronously?
easy
A. send_email.delay()
B. send_email()
C. send_email.async()
D. send_email.run()

Solution

  1. Step 1: Recall Celery task calling syntax

    Celery tasks are called asynchronously using the .delay() method.
  2. Step 2: Match the correct method to the task name

    Calling send_email.delay() queues the task to run in the background.
  3. Final Answer:

    send_email.delay() -> Option A
  4. Quick Check:

    Use .delay() to call async tasks [OK]
Hint: Use .delay() to call Celery tasks asynchronously [OK]
Common Mistakes:
  • Calling the task like a normal function without .delay()
  • Using .async() which is not a Celery method
  • Using .run() which executes task synchronously
3. Given the following Celery task and call:
@shared_task
def add(x, y):
    return x + y

result = add.delay(4, 5)

What will result.get() return after the task completes?
medium
A. 9
B. None
C. An error because .get() is not valid
D. A task ID string

Solution

  1. Step 1: Understand what add.delay(4, 5) does

    This queues the add task with arguments 4 and 5 to run asynchronously.
  2. Step 2: Understand result.get() usage

    Calling result.get() waits for the task to finish and returns the result, which is 4 + 5 = 9.
  3. Final Answer:

    9 -> Option A
  4. Quick Check:

    Task result = 4 + 5 = 9 [OK]
Hint: Use .get() to fetch async task result after completion [OK]
Common Mistakes:
  • Thinking .get() returns task ID instead of result
  • Believing .get() is invalid for Celery AsyncResult
  • Expecting None because task runs asynchronously
4. What is wrong with this Celery task call?
@shared_task
def multiply(x, y):
    return x * y

result = multiply(3, 7)
medium
A. The arguments 3 and 7 are invalid types
B. The task is missing the @app.task decorator
C. The function multiply should not return a value
D. The task is called synchronously, not asynchronously

Solution

  1. Step 1: Check how the task is called

    The task is called directly as multiply(3, 7), which runs it immediately and blocks.
  2. Step 2: Identify correct async call method

    To call asynchronously, it should be multiply.delay(3, 7).
  3. Final Answer:

    The task is called synchronously, not asynchronously -> Option D
  4. Quick Check:

    Direct call runs sync, use .delay() for async [OK]
Hint: Use .delay() to call tasks async, not direct function call [OK]
Common Mistakes:
  • Calling task function directly instead of using .delay()
  • Confusing @shared_task with @app.task (both valid but different)
  • Thinking return values are disallowed in tasks
5. You want to send a welcome email asynchronously after a user registers. Which approach correctly combines Django signals and Celery tasks to achieve this?
hard
A. Call send_welcome_email(user.id) directly inside the signal handler
B. Connect a signal handler that calls send_welcome_email.delay(user.id) after user creation
C. Use a signal to call send_welcome_email.run(user.id) synchronously
D. Call send_welcome_email.delay() before the user is saved

Solution

  1. Step 1: Understand the goal

    We want to send the email asynchronously after the user is created to avoid slowing registration.
  2. Step 2: Use Django signals with Celery correctly

    Connect a signal (like post_save) to call send_welcome_email.delay(user.id) after user creation, queuing the task.
  3. Final Answer:

    Connect a signal handler that calls send_welcome_email.delay(user.id) after user creation -> Option B
  4. Quick Check:

    Signal + .delay() after save = async email send [OK]
Hint: Use signals to trigger .delay() after user save [OK]
Common Mistakes:
  • Calling task directly inside signal, blocking request
  • Calling .run() which is synchronous
  • Calling .delay() before user exists in DB