0
0
Djangoframework~20 mins

Calling tasks asynchronously in Django - Practice Problems & Coding Challenges

Choose your learning style9 modes available
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.