0
0
Djangoframework~20 mins

Defining tasks in Django - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Celery Task Master
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 when called synchronously?
Consider this Django Celery task definition and its synchronous call. What will be printed?
Django
from celery import shared_task

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

result = add(4, 5)
print(result)
ARaises TypeError because task is not awaited
BPrints <AsyncResult: ...>
CPrints 9
DPrints None
Attempts:
2 left
💡 Hint
Calling the task function directly runs it like a normal function.
state_output
intermediate
2:00remaining
What is the type of result when calling a Celery task asynchronously?
Given this task call, what is the type of the variable 'result'?
Django
from celery import shared_task

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

result = multiply.delay(3, 7)
print(type(result))
A<class 'celery.result.AsyncResult'>
B<class 'int'>
C<class 'NoneType'>
D<class 'str'>
Attempts:
2 left
💡 Hint
Using .delay() returns a special object representing the task execution.
📝 Syntax
advanced
2:30remaining
Which option correctly defines a periodic task in Django Celery?
Select the correct way to define a periodic task that runs every 10 minutes using Celery beat.
A
@shared_task
def cleanup():
    pass

app.conf.beat_schedule = {
    'cleanup-every-10-minutes': {
        'task': 'app.tasks.cleanup',
        'schedule': timedelta(minutes=10),
    },
}
B
@shared_task
@periodic_task(run_every=10)
def cleanup():
    pass
C
@shared_task
def cleanup():
    pass

app.conf.beat_schedule = {
    'cleanup-every-10-minutes': {
        'task': 'app.tasks.cleanup',
        'schedule': 600,
    },
}
D
@shared_task
@periodic_task(run_every=timedelta(minutes=10))
def cleanup():
    pass
Attempts:
2 left
💡 Hint
The schedule value must be a timedelta object, not an integer.
🔧 Debug
advanced
2:30remaining
Why does this Celery task raise an ImportError when called?
Given this task code, why does calling task.delay() raise ImportError: No module named 'myapp.tasks'?
Django
from celery import shared_task

@shared_task
def send_email():
    from myapp.utils import send
    send()

# Called from another module
send_email.delay()
ACelery tasks cannot import inside the function body
BThe import inside the task function causes circular import
CThe task decorator is missing parentheses
DThe task module is not in the Python path or installed apps
Attempts:
2 left
💡 Hint
Check if the module 'myapp.tasks' is discoverable by Celery worker.
🧠 Conceptual
expert
3:00remaining
What happens if a Celery task raises an exception during execution?
When a Celery task raises an exception, which of the following is true about the task state and result?
AThe task silently retries without changing state
BThe task state is set to 'FAILURE' and the exception info is stored in the result
CThe task state remains 'PENDING' and no error info is stored
DThe task state is set to 'SUCCESS' but result is None
Attempts:
2 left
💡 Hint
Celery tracks task failures explicitly.