Flask - Background TasksYou 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 handlerBRun the Redis pubsub.listen() loop inside a background threadCUse Redis publish() inside the listen() loopDSubscribe to Redis channels without calling listen()Check Answer
Step-by-Step SolutionSolution:Step 1: Identify blocking issue with listen()Calling listen() in the main thread blocks Flask, freezing the app.Step 2: Use background thread for listeningRunning listen() in a separate thread allows Flask to stay responsive while listening for messages.Final Answer:Run the Redis pubsub.listen() loop inside a background thread -> Option BQuick Check:Background thread for listen() = no freeze [OK]Quick Trick: Use background thread to avoid blocking Flask [OK]Common Mistakes:MISTAKESRunning listen() in main route causing freezeMisusing publish() inside listen() loopSubscribing without listen() so no messages received
Master "Background Tasks" in Flask9 interactive learning modes - each teaches the same concept differentlyLearnWhyDeepVisualTryChallengeProjectRecallPerf
More Flask Quizzes Deployment - Docker containerization - Quiz 1easy Deployment - Environment variable management - Quiz 8hard Middleware and Extensions - WSGI middleware concept - Quiz 2easy Middleware and Extensions - Flask-Compress for compression - Quiz 7medium Performance Optimization - Connection pooling - Quiz 13medium Performance Optimization - Static file optimization - Quiz 15hard Security Best Practices - Rate limiting for protection - Quiz 3easy Testing Flask Applications - Testing forms and POST data - Quiz 13medium Testing Flask Applications - Testing routes and responses - Quiz 14medium WebSocket and Real-Time - Broadcasting to clients - Quiz 13medium