0
0
Flaskframework~10 mins

Celery integration overview in Flask - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import Celery in a Flask app.

Flask
from flask import Flask
from [1] import Celery

app = Flask(__name__)
celery = Celery(app.name, broker='redis://localhost:6379/0')
Drag options to blanks, or click blank then click option'
Acelery
Bflask_celery
Cflaskext
Dflask_celery_worker
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to import Celery from 'flask_celery' or other non-existent packages.
Forgetting to import Celery at all.
2fill in blank
medium

Complete the code to define a Celery task in Flask.

Flask
@celery.task
def [1](x, y):
    return x + y
Drag options to blanks, or click blank then click option'
Acalculate
Badd_numbers
Csum
Dtask
Attempts:
3 left
💡 Hint
Common Mistakes
Using Python built-in names like 'sum' as function names.
Not decorating the function with @celery.task.
3fill in blank
hard

Fix the error in the Celery configuration for Flask.

Flask
app.config.update(
    CELERY_BROKER_URL='[1]',
    CELERY_RESULT_BACKEND='redis://localhost:6379/0'
)

celery.conf.update(app.config)
Drag options to blanks, or click blank then click option'
Ahttp://localhost:6379
Bredis://127.0.0.1:6379/0
Camqp://guest@localhost//
Dredis://localhost:6379/0
Attempts:
3 left
💡 Hint
Common Mistakes
Using Redis URL as broker when RabbitMQ is intended.
Using HTTP URLs which are invalid for brokers.
4fill in blank
hard

Fill both blanks to create a Celery instance with Flask app context.

Flask
def make_celery(app):
    celery = Celery(app.import_name, broker=app.config['[1]'])
    celery.conf.update(app.config)

    class ContextTask(celery.Task):
        def __call__(self, *args, **kwargs):
            with app.app_context():
                return self.run(*args, **kwargs)

    celery.Task = [2]
    return celery
Drag options to blanks, or click blank then click option'
ACELERY_BROKER_URL
BCELERY_RESULT_BACKEND
CContextTask
DTask
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong config key for the broker URL.
Assigning the wrong class to celery.Task.
5fill in blank
hard

Fill all three blanks to call a Celery task asynchronously and get the result.

Flask
result = [1].delay(10, 20)
output = result.[2]()
print('Task result:', [3])
Drag options to blanks, or click blank then click option'
Aadd_numbers
Bget
Coutput
Dresult
Attempts:
3 left
💡 Hint
Common Mistakes
Calling the task function directly instead of using delay.
Using wrong variable names for the result or output.