Bird
0
0

Given this Flask code snippet, what will be printed?

medium📝 component behavior Q4 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'
AOnly 'Task done' printed, no 'Hello'
BBoth 'Hello' and 'Task done' printed at the same time synchronously
CHello printed immediately, then 'Task done' printed asynchronously
DNo output printed because thread is not started
Step-by-Step Solution
Solution:
  1. Step 1: Understand threading in Flask route

    The route starts a background thread that prints 'Task done' asynchronously.
  2. Step 2: Identify response behavior

    The route returns 'Hello' immediately without waiting for the thread.
  3. Final Answer:

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

    Threaded task prints later, response returns immediately [OK]
Quick Trick: Thread starts async, response returns immediately [OK]
Common Mistakes:
MISTAKES
  • Expecting 'Task done' before 'Hello'
  • Thinking thread blocks response
  • Assuming no output because thread is ignored

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Flask Quizzes