Bird
0
0

You wrote this code to subscribe to Redis messages but your Django app freezes. What is the likely problem?

medium📝 Debug Q14 of 15
Django - Celery and Background Tasks
You wrote this code to subscribe to Redis messages but your Django app freezes. What is the likely problem?
import redis

r = redis.Redis()

pubsub = r.pubsub()
pubsub.subscribe('notifications')

for message in pubsub.listen():
    print(message)
AThe subscribe() method is missing a callback function
BThe print statement syntax is incorrect
CRedis server is not running, causing freeze
DThe listen() loop blocks the main thread, freezing the app
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the listen() method behavior

    The listen() method blocks and waits for messages, running an infinite loop.
  2. Step 2: Understand impact on Django app

    Running this loop in the main thread freezes the app because it never returns control.
  3. Final Answer:

    The listen() loop blocks the main thread, freezing the app -> Option D
  4. Quick Check:

    Blocking listen() causes freeze [OK]
Quick Trick: Run listen() in background thread to avoid blocking [OK]
Common Mistakes:
MISTAKES
  • Thinking subscribe() needs a callback
  • Assuming Redis server down causes freeze
  • Misreading print syntax as error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Django Quizzes