Bird
0
0

You want to listen for Redis messages in a Flask app without freezing it. Which approach correctly solves this problem?

hard📝 Application Q15 of 15
Flask - Background Tasks
You want to listen for Redis messages in a Flask app without freezing it. Which approach correctly solves this problem?
ACall pubsub.listen() directly in the main Flask route handler
BRun the Redis pubsub.listen() loop inside a background thread
CUse Redis publish() inside the listen() loop
DSubscribe to Redis channels without calling listen()
Step-by-Step Solution
Solution:
  1. Step 1: Identify blocking issue with listen()

    Calling listen() in the main thread blocks Flask, freezing the app.
  2. Step 2: Use background thread for listening

    Running listen() in a separate thread allows Flask to stay responsive while listening for messages.
  3. Final Answer:

    Run the Redis pubsub.listen() loop inside a background thread -> Option B
  4. Quick Check:

    Background thread for listen() = no freeze [OK]
Quick Trick: Use background thread to avoid blocking Flask [OK]
Common Mistakes:
MISTAKES
  • Running listen() in main route causing freeze
  • Misusing publish() inside listen() loop
  • Subscribing without listen() so no messages received

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Flask Quizzes