在 Redis 中使用管道进行批量处理(python 示例)

using pipeline for bulk processing in redis (python example)

我正在尝试使用 python 客户端清除我在 redis 中实现的流水线概念。举个例子:

my_server = redis.Redis(connection_pool=POOL)
for obj_id in list_of_obj_ids:
    hash_name="n:"+str(obj_id)
    sorted_set = "s:"+str(obj_id)
    if my_server.exists(hash_name):
        my_server.hset(hash_name,'val',0)
    if my_server.zcard(sorted_set):
        my_server.zadd(sorted_set, hash_name, time.time())

即我正在通过遍历 for 循环来更新多个哈希值。我怎样才能通过流水线完成这种批量更新?根据我的阅读,以下是我的想法:

my_server = redis.Redis(connection_pool=POOL)
p = my_server.pipeline()
for obj_id in list_of_obj_ids:
    hash_name="n:"+str(obj_id)
    sorted_set="s:"+str(obj_id)
    if p.exists(hash_name):
        p.hset(hash_name,'val',0)
    if p.zcard(sorted_set):
        p.zadd(sorted_set, hash_name, time.time())
p.execute()

这是正确的吗?

阅读有关 pipelining is/does 的内容,然后您就会明白为什么这行不通 - 直到您 execute 管道,none 中的命令它将被发送到服务器。这会使您的条件语句无法达到您的目的。