Complete the code to import Celery in a Flask app.
from flask import Flask from [1] import Celery app = Flask(__name__) celery = Celery(app.name, broker='redis://localhost:6379/0')
You import Celery directly from the celery package to use it with Flask.
Complete the code to define a Celery task in Flask.
@celery.task def [1](x, y): return x + y
The function name can be anything, but add_numbers clearly describes the task.
Fix the error in the Celery configuration for Flask.
app.config.update(
CELERY_BROKER_URL='[1]',
CELERY_RESULT_BACKEND='redis://localhost:6379/0'
)
celery.conf.update(app.config)The broker URL for Celery using RabbitMQ is amqp://guest@localhost//. Redis URLs start with redis://.
Fill both blanks to create a Celery instance with Flask app context.
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
The broker URL is stored in CELERY_BROKER_URL. The custom task class ContextTask ensures Flask app context during task execution.
Fill all three blanks to call a Celery task asynchronously and get the result.
result = [1].delay(10, 20) output = result.[2]() print('Task result:', [3])
You call the task function add_numbers with delay to run asynchronously. Then use get() on the result to wait and fetch the output. Finally, print the output variable.