Bird
0
0

Identify the issue in this Redis subscription code snippet used in Django: ```python pubsub = redis_client.pubsub() pubsub.subscribe('news') message = pubsub.get_message() print(message['data']) ```

medium📝 Debug Q7 of 15
Django - Celery and Background Tasks
Identify the issue in this Redis subscription code snippet used in Django: ```python pubsub = redis_client.pubsub() pubsub.subscribe('news') message = pubsub.get_message() print(message['data']) ```
Asubscribe() should be called after get_message()
Bget_message() may return None if no message is available, causing a TypeError when accessing ['data']
CThe channel name 'news' must be bytes, not string
Dredis_client.pubsub() does not exist; should use redis_client.subscribe()
Step-by-Step Solution
Solution:
  1. Step 1: Check get_message return value

    get_message() returns None if no message is ready.
  2. Step 2: Accessing ['data'] on None

    Trying to access ['data'] on None causes a TypeError.
  3. Step 3: Verify other options

    subscribe() must be called before get_message(); channel names can be strings; pubsub() is correct method.
  4. Final Answer:

    get_message() may return None if no message is available, causing a TypeError when accessing ['data'] -> Option B
  5. Quick Check:

    Always check if get_message() is None before accessing data [OK]
Quick Trick: Check for None before accessing message data [OK]
Common Mistakes:
MISTAKES
  • Not handling None return from get_message()
  • Calling subscribe after get_message()
  • Assuming channel names must be bytes
  • Using incorrect pubsub method

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Django Quizzes