Bird
0
0

You want to monitor multiple background tasks in Flask and update their status safely. Which approach is best to store and update task states?

hard📝 Best Practice Q15 of 15
Flask - Background Tasks
You want to monitor multiple background tasks in Flask and update their status safely. Which approach is best to store and update task states?
AStore task states only in local variables inside routes
BStore task states in a file without synchronization
CUse print statements to track task progress in console
DUse a global dictionary with thread locks to store task states
Step-by-Step Solution
Solution:
  1. Step 1: Consider concurrency in Flask background tasks

    Multiple tasks may update states concurrently, so thread safety is needed.
  2. Step 2: Evaluate storage options

    Global dictionary with thread locks ensures safe updates; local vars or files without sync cause errors.
  3. Final Answer:

    Use a global dictionary with thread locks to store task states -> Option D
  4. Quick Check:

    Safe shared state needs locks [OK]
Quick Trick: Use locks with shared dict for safe task status updates [OK]
Common Mistakes:
MISTAKES
  • Using local variables which don't persist
  • Ignoring thread safety causing race conditions
  • Relying on print statements for monitoring

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Flask Quizzes