Bird
0
0

What will happen when this Flask route is called?

medium📝 component behavior Q13 of 15
Flask - Background Tasks
What will happen when this Flask route is called?
import threading
from flask import Flask
app = Flask(__name__)
def slow_task():
    import time
    time.sleep(5)
    print('Task done')
@app.route('/')
def index():
    threading.Thread(target=slow_task).start()
    return 'Request received'
AThe server crashes due to threading error.
BThe server waits 5 seconds before sending 'Request received'.
CThe response 'Request received' is sent immediately, and 'Task done' prints after 5 seconds.
DNothing prints because the thread never starts.
Step-by-Step Solution
Solution:
  1. Step 1: Analyze threading behavior in route

    The slow_task runs in a new thread, so the main thread returns response immediately.
  2. Step 2: Understand output timing

    'Request received' is returned right away; 'Task done' prints after 5 seconds when slow_task finishes.
  3. Final Answer:

    The response 'Request received' is sent immediately, and 'Task done' prints after 5 seconds. -> Option C
  4. Quick Check:

    Background thread runs separately, response immediate [OK]
Quick Trick: Thread runs slow task separately; response returns fast [OK]
Common Mistakes:
MISTAKES
  • Thinking server waits for slow_task to finish
  • Assuming thread causes server crash
  • Believing thread never starts without join()

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Flask Quizzes