不幸的是,redis-py库似乎没有Monitor例程。我想阅读redis服务器收到的所有命令,过滤它们,然后记录我感兴趣的命令。有没有人知道如何做到这一点?
答案 0 :(得分:8)
这是在python中实现监视器代码的一些最小代码。
注意:
import redis class Monitor(): def __init__(self, connection_pool): self.connection_pool = connection_pool self.connection = None def __del__(self): try: self.reset() except: pass def reset(self): if self.connection: self.connection_pool.release(self.connection) self.connection = None def monitor(self): if self.connection is None: self.connection = self.connection_pool.get_connection( 'monitor', None) self.connection.send_command("monitor") return self.listen() def parse_response(self): return self.connection.read_response() def listen(self): while True: yield self.parse_response() if __name__ == '__main__': pool = redis.ConnectionPool(host='localhost', port=6379, db=0) monitor = Monitor(pool) commands = monitor.monitor() for c in commands : print(c)
答案 1 :(得分:1)
现在,redis库已经包含了监视器支持本身。 https://github.com/andymccurdy/redis-py/blob/master/redis/client.py#L3422