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

标签 python 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

关于python - 如何将redis管道用于嵌套方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33981911/

相关文章:

serialization - redis 不同语言的客户端可以混合使用吗?

redis - 在 Redis 中使用持久性时, key 过期是否尊重原始时间?

python - Odoo v10 API 与 Odoo v8 API

python - 如何填充稀疏的 Pandas DataFrame,以便每个标识符组合每周都用填充值 0 表示?

Python 请求错误 10060

ruby - 生产服务器上的 Sidekiq 在运行时出错

mongodb - 哪些 NoSQL 数据库(如果有)可以为查询结果集提供 *changes* 流?

python - 如何在 PyQt 上绘制合适的网格?

python - 在某些单词后拆分或分区字符串

networking - 从 google tcp 负载均衡器检测 redis master 的健康检查