0
0
Djangoframework~10 mins

Redis as message broker in Django - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the Redis client in Django.

Django
from redis import [1]
Drag options to blanks, or click blank then click option'
ARedis
BClient
CConnection
DBroker
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Client' instead of 'Redis' causes import errors.
Trying to import 'Broker' which does not exist in redis package.
2fill in blank
medium

Complete the code to publish a message to a Redis channel.

Django
redis_client = Redis()
redis_client.[1]('channel1', 'Hello World')
Drag options to blanks, or click blank then click option'
Abroadcast
Bpush
Csend
Dpublish
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'send' or 'push' which are not Redis client methods.
Trying to use 'broadcast' which is not a Redis method.
3fill in blank
hard

Fix the error in subscribing to a Redis channel.

Django
pubsub = redis_client.[1]()
pubsub.subscribe('channel1')
Drag options to blanks, or click blank then click option'
Apubsub()
Bsubscribe
Cpubsub
Dpubsub_subscribe
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to call 'subscribe()' directly on the Redis client.
Using 'subscribe' as a method to create pubsub object.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps channel names to message counts, filtering only channels with more than 5 messages.

Django
message_counts = {channel: count for channel, count in redis_client.[1]('channel_counts').items() if count [2] 5}
Drag options to blanks, or click blank then click option'
Ahgetall
B>
C<
Dkeys
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'keys()' which returns only keys, not counts.
Using '<' which filters counts less than 5.
5fill in blank
hard

Fill all three blanks to create a Redis message handler that listens to a channel, decodes messages, and prints them.

Django
pubsub = redis_client.[1]()
pubsub.subscribe('[2]')
for message in pubsub.listen():
    if message['type'] == 'message':
        print(message['data'].[3]('utf-8'))
Drag options to blanks, or click blank then click option'
Apubsub
Bchannel1
Cdecode
Dsubscribe
Attempts:
3 left
💡 Hint
Common Mistakes
Calling 'subscribe' instead of 'pubsub()' to create the pubsub object.
Not decoding the message data before printing.