Per the documentation (https://github.com/andymccurdy/redis-py#publish--subscribe), when calling listen()
, the message
is returned as a dictionary:
- type: One of the following: 'subscribe', 'unsubscribe', 'psubscribe', 'punsubscribe', 'message', 'pmessage'
- channel: The channel [un]subscribed to or the channel a message was published to
- pattern: The pattern that matched a published message's channel. Will be None in all cases except for 'pmessage' types.
- data: The message data. With [un]subscribe messages, this value will be the number of channels and patterns the connection is currently subscribed to. With [p]message messages, this value will be the actual published message.
In the case of the '__keyspace*' pattern, the data
key in the message
dict holds the operation's name. Put differently:
>>> p.psubscribe("__keyspace@{}__:*")
>>> for message in p.listen():
... print(message['data']) # print the operation
Note: the call to format(...)
appears to be not needed in your sample.