Bird
Raised Fist0
Flaskframework~20 mins

Defining Celery tasks in Flask - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Celery 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 Celery task call?

Given this Celery task defined in a Flask app, what will result.get() return?

Flask
from celery import Celery

app = Celery('tasks', broker='redis://localhost:6379/0')

@app.task
def add(x, y):
    return x + y

result = add.delay(4, 5)
AReturns AsyncResult object
B9
CRaises AttributeError
DNone
Attempts:
2 left
💡 Hint

Remember that delay() queues the task and get() waits for the result.

📝 Syntax
intermediate
2:00remaining
Which option correctly defines a Celery task in Flask?

Choose the correct way to define a Celery task function in a Flask app.

A
@app.task
def multiply(x, y):
    return x * y
B
def multiply(x, y):
    return x * y
@app.task
C
@app.task()
def multiply(x, y):
    return x * y
D
@app.task
def multiply(x, y):
    print(x * y)
Attempts:
2 left
💡 Hint

Decorator syntax must be directly above the function definition without parentheses if no arguments.

❓ state_output
advanced
2:00remaining
What is the value of result.status immediately after calling add.delay(2, 3)?

Consider this Celery task call:

result = add.delay(2, 3)

What is the value of result.status right after this line?

Flask
from celery import Celery

app = Celery('tasks', broker='redis://localhost:6379/0')

@app.task
def add(x, y):
    return x + y

result = add.delay(2, 3)
A"STARTED"
B"SUCCESS"
C"FAILURE"
D"PENDING"
Attempts:
2 left
💡 Hint

Think about the task lifecycle immediately after queuing.

🔧 Debug
advanced
2:00remaining
Why does this Celery task raise an error?

Given this task definition, why does calling multiply.delay(3, 4) raise an error?

Flask
from celery import Celery

app = Celery('tasks', broker='redis://localhost:6379/0')

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

app.task(multiply)
AThe task decorator is not applied correctly; it should be @app.task above the function
BThe broker URL is invalid causing connection failure
CThe function multiply is missing a return statement
DThe task name is missing causing registration failure
Attempts:
2 left
💡 Hint

Check how the task decorator is used in Celery.

🧠 Conceptual
expert
3:00remaining
Which option best explains how Celery tasks are discovered in a Flask app?

In a Flask app using Celery, how does Celery find and register tasks defined in different modules?

ATasks must be explicitly imported in the Celery app module to be registered
BCelery uses a configuration setting to list all task names manually
CCelery auto-discovers tasks by scanning all Python files in the project directory
DTasks are registered only if decorated with @app.task and placed in the main app file
Attempts:
2 left
💡 Hint

Think about Python import behavior and how decorators work.

Practice

(1/5)
1. What is the main purpose of defining a Celery task in a Flask application using @celery_app.task?
easy
A. To make the Flask app run faster by disabling routes
B. To automatically restart the Flask server
C. To run functions asynchronously in the background
D. To create database models

Solution

  1. Step 1: Understand Celery task decorator

    The @celery_app.task decorator marks a function to run as a background task outside the main Flask process.
  2. Step 2: Purpose of asynchronous tasks

    This allows long-running or slow tasks to run without blocking the main app, improving responsiveness.
  3. Final Answer:

    To run functions asynchronously in the background -> Option C
  4. Quick Check:

    Celery tasks = background async functions [OK]
Hint: Celery tasks run outside Flask to avoid blocking [OK]
Common Mistakes:
  • Thinking Celery tasks restart the server
  • Confusing tasks with database models
  • Believing tasks disable routes
2. Which of the following is the correct way to define a Celery task in Flask?
easy
A. @celery_app.task def my_task(): pass
B. celery_app.run_task(my_task)
C. @app.route('/task') def my_task(): pass
D. def my_task(): pass

Solution

  1. Step 1: Identify the decorator for Celery tasks

    The correct decorator to define a Celery task is @celery_app.task.
  2. Step 2: Check syntax correctness

    @celery_app.task def my_task(): pass correctly uses @celery_app.task above the function definition.
  3. Final Answer:

    @celery_app.task\ndef my_task(): pass -> Option A
  4. Quick Check:

    Use @celery_app.task to define tasks [OK]
Hint: Look for @celery_app.task decorator above function [OK]
Common Mistakes:
  • Using Flask route decorator instead of Celery task
  • Calling run_task instead of using decorator
  • Defining function without decorator
3. Given the following code, what will be the output when add.delay(4, 5) is called?
from celery import Celery
celery_app = Celery('tasks')

@celery_app.task
def add(x, y):
    return x + y

result = add.delay(4, 5)
print(type(result))
medium
A. <class 'celery.result.AsyncResult'>
B. <class 'int'>
C. 9
D. None

Solution

  1. Step 1: Understand what delay() returns

    The delay() method queues the task and returns an AsyncResult object, not the actual result.
  2. Step 2: Check the printed type

    Printing type(result) shows the class of the returned object, which is celery.result.AsyncResult.
  3. Final Answer:

    <class 'celery.result.AsyncResult'> -> Option A
  4. Quick Check:

    delay() returns AsyncResult object [OK]
Hint: delay() returns AsyncResult, not the task output [OK]
Common Mistakes:
  • Expecting delay() to return the task result immediately
  • Confusing AsyncResult with the actual return value
  • Printing result instead of type(result)
4. Identify the error in the following Celery task definition:
from celery import Celery
celery_app = Celery('tasks')

@celery_app.task()
def multiply(x, y):
    return x * y

result = multiply.delay(3, 7)
print(result.get())
medium
A. Using parentheses in @celery_app.task() decorator causes error
B. Calling delay() instead of apply_async() is wrong
C. Missing Flask app context inside the task
D. No error; code runs correctly

Solution

  1. Step 1: Check decorator syntax

    Using parentheses in @celery_app.task() is allowed and works correctly.
  2. Step 2: Verify delay() usage and result retrieval

    Calling delay() queues the task, and result.get() waits for and returns the result properly.
  3. Final Answer:

    No error; code runs correctly -> Option D
  4. Quick Check:

    Parentheses in decorator are optional and valid [OK]
Hint: Parentheses in @celery_app.task() are optional [OK]
Common Mistakes:
  • Thinking parentheses cause syntax error
  • Confusing delay() with apply_async()
  • Assuming Flask context is always needed in tasks
5. You want to define a Celery task in Flask that sends emails but only if the recipient email is not empty. Which of the following task definitions correctly applies this condition inside the task?
hard
A. @celery_app.task def send_email(email): if email is not None: print(f"Sending email to {email}")
B. @celery_app.task def send_email(email): if not email: return print(f"Sending email to {email}")
C. @celery_app.task def send_email(email): if email is None: print("No email provided") else: print(f"Sending email to {email}")
D. @celery_app.task def send_email(email): print(f"Sending email to {email}")

Solution

  1. Step 1: Understand the condition to skip empty emails

    The task should not send emails if the email is empty or falsy (like empty string).
  2. Step 2: Check which option correctly skips empty emails

    @celery_app.task def send_email(email): if not email: return print(f"Sending email to {email}") uses if not email: return to exit early, preventing sending to empty emails.
  3. Final Answer:

    if not email: return to skip empty emails -> Option B
  4. Quick Check:

    Use if not email: return to skip empty input [OK]
Hint: Return early if email is empty to skip sending [OK]
Common Mistakes:
  • Not checking for empty string, only None
  • Sending email without any condition
  • Using incorrect condition that doesn't skip empty
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard