Bird
0
0

Identify the error in this Flask Redis subscription code: ```python pubsub = redis_client.pubsub() pubsub.subscribe('channel1') message = pubsub.get_message() print(message['data']) ``` If no message was published yet, what happens?

medium📝 Debug Q6 of 15
Flask - Background Tasks
Identify the error in this Flask Redis subscription code: ```python pubsub = redis_client.pubsub() pubsub.subscribe('channel1') message = pubsub.get_message() print(message['data']) ``` If no message was published yet, what happens?
ATypeError because message is None and cannot be subscripted
BNo error, prints 1
CKeyError because 'data' key is missing
DAttributeError because get_message is not a method
Step-by-Step Solution
Solution:
  1. Step 1: Understand pubsub.get_message after subscribe

    The first call to get_message() returns the subscription confirmation {'type': 'subscribe', 'channel': b'channel1', 'data': 1}.
  2. Step 2: Analyze print statement

    message['data'] is 1 (integer), so it prints 1 with no error.
  3. Final Answer:

    No error, prints 1 -> Option B
  4. Quick Check:

    Subscription confirmation has 'data': 1 [OK]
Quick Trick: First get_message after subscribe returns confirmation with data=1 [OK]
Common Mistakes:
MISTAKES
  • Forgetting subscription confirmation message
  • Assuming get_message returns None immediately
  • Expecting KeyError instead of no error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Flask Quizzes