Bird
0
0

Which method best uses background processing to achieve this?

hard📝 Application Q8 of 15
Flask - Background Tasks
You want to process image uploads and send a notification email after the upload completes in a Flask app without delaying the HTTP response. Which method best uses background processing to achieve this?
AStart a background thread to handle image processing and email sending after returning the response
BProcess images and send emails synchronously inside the route before returning
CUse Flask's <code>after_request</code> decorator to run the tasks
DRun image processing and email sending in the main thread but with a timeout
Step-by-Step Solution
Solution:
  1. Step 1: Understand synchronous vs background processing

    Running long tasks synchronously delays the HTTP response, degrading user experience.
  2. Step 2: Identify best background approach

    Starting a background thread allows the route to return immediately while tasks run asynchronously.
  3. Step 3: Why other options are incorrect

    • after_request runs after response but is still synchronous and blocks response.
    • Running tasks in main thread with timeout still blocks response.
  4. Final Answer:

    Start a background thread to handle image processing and email sending after returning the response -> Option A
  5. Quick Check:

    Background threads improve responsiveness for long tasks [OK]
Quick Trick: Use background threads to avoid blocking HTTP responses [OK]
Common Mistakes:
MISTAKES
  • Running long tasks synchronously inside routes
  • Misusing after_request for async processing
  • Assuming timeouts prevent blocking

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Flask Quizzes