Bird
0
0

Given this Flask code snippet, what will be printed?

medium📝 component behavior Q13 of 15
Flask - Background Tasks
Given this Flask code snippet, what will be printed?
from threading import Thread
from flask import Flask
app = Flask(__name__)
def background_task():
    print('Task done')
@app.route('/')
def index():
    Thread(target=background_task).start()
    return 'Hello'
AHello printed immediately, then 'Task done' printed later
BOnly 'Hello' is printed, 'Task done' never prints
CBoth 'Hello' and 'Task done' print at the same time
DSyntaxError because Thread is used incorrectly
Step-by-Step Solution
Solution:
  1. Step 1: Understand thread behavior in Flask route

    The route starts a thread to run background_task, then returns 'Hello' immediately.
  2. Step 2: Predict output order

    'Hello' is sent to user right away; 'Task done' prints later when thread runs.
  3. Final Answer:

    Hello printed immediately, then 'Task done' printed later -> Option A
  4. Quick Check:

    Thread runs task after response = A [OK]
Quick Trick: Thread runs task after response, so print order differs [OK]
Common Mistakes:
MISTAKES
  • Thinking 'Task done' prints before 'Hello'
  • Assuming thread blocks response
  • Believing thread syntax is wrong here

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Flask Quizzes