Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to import Celery in your Django project.
Django
from celery import [1]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Importing 'app' or 'task' instead of 'Celery'.
Using lowercase 'celery'.
✗ Incorrect
You import Celery class from the celery package to create a Celery app instance.
2fill in blank
mediumComplete the code to create a Celery app instance named 'app'.
Django
app = Celery('[1]')
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'celery_app' or 'tasks' which are not project names.
Using 'django_project' as a generic name.
✗ Incorrect
The argument is usually your Django project name, here 'myproject'.
3fill in blank
hardFix the error in the code to load Django settings into Celery.
Django
app.config_from_object('[1]')
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using dot notation instead of colon.
Using just 'settings' without module.
✗ Incorrect
The correct string to load Django settings is 'django.conf:settings'.
4fill in blank
hardFill both blanks to auto-discover tasks in Django apps.
Django
app.[1]_tasks() # Auto-discover tasks in installed apps app.conf.broker_url = '[2]'
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'discover_tasks' instead of 'autodiscover_tasks'.
Using AMQP URL when Redis is expected.
✗ Incorrect
autodiscover_tasks() finds tasks.py files in apps.
The broker URL here uses Redis on localhost.
5fill in blank
hardFill all three blanks to define a simple Celery task function.
Django
from celery import shared_task @shared_task def [1](x, y): return x [2] y # Returns sum result = [3](4, 5).delay()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'sum' as function name which is a built-in function.
Using '-' or '*' instead of '+'.
✗ Incorrect
The task function is named 'add'. It returns the sum using '+'.
The task is called by its name 'add' with .delay() to run asynchronously.