在 redis
中,它说:
A Redis transaction is entered using the MULTI command.
对于它的 Python
API:
r = redis.Redis(...)
pipe = r.pipeline()
current_value = pipe.get('someKey')
#pipe.multi()
pipe.set('someKey', current_value + 1)
pipe.execute()
使用和不使用 pipe.multi()
有什么区别?
要保证其原子性,正确的解决方案是什么?
最佳答案
流水线
是一种可以节省(RTT) 往返时间的机制,当您实际上想要批处理 key 的更新/查询并且您不需要每个键本身的回复。
Multi
实际上会使事务原子化;所以一组命令被组合成一个命令,example会是:
MULTI
SADD foo a
SADD foo b
EXEC
如果有人在事务发生之前或期间查询 foo
的值,他们将得到 NULL
而有人在事务完成后查询将得到 a ,b
,因此您不可能单独获得 a
。
Python/Redis API 引用默认将其设置为 MULTI
,如果不需要,您可以将其设置为 false,MULTI/EXEC:这些是作为流水线类。管道在执行时默认使用 MULTI 和 EXEC 语句包装,可以通过指定 transaction=False 来禁用。
Reference
关于python - Python 中的 Redis : difference of with and without multi() function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33695837/