Consider a Flask app using Redis as a message broker. When the app publishes a message to a Redis channel, what is the immediate effect?
import redis r = redis.Redis() r.publish('channel1', 'Hello')
Think about how Redis Pub/Sub works with channels and subscribers.
Redis Pub/Sub sends messages instantly to all clients subscribed to the channel. It does not store messages for later delivery or save them in lists.
Given this Flask code snippet subscribing to a Redis channel, what will be printed when a message is published?
import redis r = redis.Redis() ps = r.pubsub() ps.subscribe('channel1') for message in ps.listen(): if message['type'] == 'message': print(message['data'].decode()) break
Remember the message dictionary keys and their meanings in Redis Pub/Sub.
The 'message' type contains the actual published data. Decoding it prints the original string message.
Choose the correct way to set up Redis Pub/Sub in a Flask app to listen to a channel.
Check the Redis Python client API for Pub/Sub setup.
Option A correctly creates a Redis client, then a Pub/Sub object, then subscribes to a channel.
Given this code, why does the subscriber never print any messages?
import redis r = redis.Redis() ps = r.pubsub() ps.subscribe('channel1') for message in ps.listen(): if message['type'] == 'message': print(message['data'].decode())
Consider the data type of message['data'] and how print handles it.
Redis messages are bytes; printing bytes shows raw bytes, which may appear empty or unreadable. Decoding to string is needed.
Identify the main limitation of Redis Pub/Sub when used as a message broker for Flask applications.
Think about message delivery guarantees and offline subscribers.
Redis Pub/Sub does not persist messages. Subscribers must be connected to receive messages; otherwise, messages are lost.