Python中的Redis:使用multi()函数与不使用multi()函数的区别

2024-04-26 10:20:34 发布

您现在位置:Python中文网/ 问答频道 /正文

redis中,它说:

A Redis transaction is entered using the MULTI command.

对于其PythonAPI:

r = redis.Redis(...)
pipe = r.pipeline()
current_value = pipe.get('someKey')
#pipe.multi()
pipe.set('someKey', current_value + 1)
pipe.execute()

有和没有pipe.multi()的区别是什么?在

为了保证原子性,正确的解决方案是什么?在


Tags: theredispythonapiisvaluecurrentmulticommand
1条回答
网友
1楼 · 发布于 2024-04-26 10:20:34

Pipelining是一种机制,可以节省(RTT)往返时间,当您实际想要批量更新/查询密钥时,您不需要为每个密钥单独返回回复。在

Multi实际上将使事务原子化;因此,将一组命令组合成一个命令,example将是:

MULTI 
SADD foo a 
SADD foo b 
EXEC 

如果有人在事务发生之前或期间查询foo的值,他们将得到NULL,而在事务完成后进行查询的人将得到a,b,因此您不可能自己得到{}。在

Python/redisapi引用默认将其设置为MULTI,如果不需要,可以将其设置为false,MULTI/EXEC: These are implemented as part of the Pipeline class. The pipeline is wrapped with the MULTI and EXEC statements by default when it is executed, which can be disabled by specifying transaction=False.Reference

相关问题 更多 >