如何将redis管道用于嵌套方法



我想为以下方法使用redis管道:

def func(self):
    .....
    result = redis.smembers(key)
    for i in result:
        self.other_func(i)
    if redis.scard(key) == 0:
            redis.delete(key)
def other_func(self, value):
    .....
    redis.set(key, value)    

我是这样写的,对吗?

def func(self):
    .....
    with redis.pipeline() as pipe:
        result = pipe.smembers(key)
        for i in result:
            self.other_func(i)
        if pip.scard(key) == 0:
                pip.delete(key)
def other_func(self, value):
    .....
    redis.set(key, value) 

other_func怎么样?我需要将pipe传递到此方法吗?

是的,您需要对要在同一管道中发送的所有命令使用pipe

最新更新