0

I'm using redis-py. I subscribe to redis database and read notification like this:

>>> p.psubscribe("__keyspace@{}__:*".format(...))
>>> for message in p.listen():
...     # do something with the message

I want to get the operation of the message, like DELETE or ADD, how can I do this?

Thank you~

batmancn
  • 457
  • 3
  • 15

1 Answers1

0

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.

Itamar Haber
  • 47,336
  • 7
  • 91
  • 117