Bird
0
0

How can you ensure Flask context is available inside a background thread started with threading.Thread?

hard📝 Application Q9 of 15
Flask - Background Tasks
How can you ensure Flask context is available inside a background thread started with threading.Thread?
AUse Flask's copy_current_request_context decorator on the background function.
BCall app.run() inside the thread.
CPass the Flask app instance as an argument to the thread target.
DBackground threads automatically have Flask context.
Step-by-Step Solution
Solution:
  1. Step 1: Understand Flask context in threads

    Flask context (request, app) is local to the main thread and not available by default in new threads.
  2. Step 2: Use copy_current_request_context decorator

    This decorator copies the current request context to the background function so it can access Flask globals.
  3. Final Answer:

    Use Flask's copy_current_request_context decorator on the background function. -> Option A
  4. Quick Check:

    copy_current_request_context = Flask context in threads [OK]
Quick Trick: Decorate background function with copy_current_request_context [OK]
Common Mistakes:
MISTAKES
  • Assuming Flask context is automatic in threads
  • Trying to run app.run() inside thread
  • Passing app instance doesn't provide context

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Flask Quizzes