Pipelines
管道
管道是Redis的子类,它支持缓冲多个命令,一次性发送到服务器去执行。可以大大的提高性能,减少服务器到客户端之间的TCP来回数据包。
管道的简单使用:
>>> r = redis.Redis(...)
>>> r.set('bing', 'baz')
>>> # Use the pipeline() method to create a pipeline instance
>>> pipe = r.pipeline()
>>> # The following SET commands are buffered
>>> pipe.set('foo', 'bar')
>>> pipe.get('bing')
>>> # the EXECUTE call sends all buffered commands to the server, returning
>>> # a list of responses, one for each command.
>>> pipe.execute()
[True, 'baz']
For ease of use, all commands being buffered into the pipeline return the pipeline object itself. Therefore calls can be chained like:
为了便于使用,可以像下面这样写。
>>> pipe.set('foo', 'bar').sadd('faz', 'baz').incr('auto_number').execute()
[True, True, 6]
In addition, pipelines can also ensure the buffered commands are executed atomically as a group. This happens by default. If you want to disable the atomic nature of a pipeline but still want to buffer commands, you can turn off transactions.
此外,管道还可以确保缓冲命令作为一组去执行。默认就是这样(就是管道开启了事务)。
要是想禁用原子性质的管道,还想缓冲命令,像下面这样设置。
>>> pipe = r.pipeline(transaction=False)
A common issue occurs when requiring atomic transactions but needing to retrieve values in Redis prior for use within the transaction. For instance, let's assume that the INCR command didn't exist and we need to build an atomic version of INCR in Python.
一个常见的问题:有些情况下需要先获得一条命令的返回值,然后在执行这个值的下一条命令,而事物中只有当所有命令都一次执行完成后才能得到每个结果的返回值。例如,假设INCR命令不存在,我们在python中实现带有原子性质的INCR命令。
The completely naive implementation could GET the value, increment it in Python, and SET the new value back. However, this is not atomic because multiple clients could be doing this at the same time, each getting the same value from GET.
通过GET获取key存在的值,然后通过SET设置新的值,但这不是原子性质的。同一时间多个客户端可以这么操作,每个都可以使用GET获取相同的值。
Enter the WATCH command. WATCH provides the ability to monitor one or more keys prior to starting a transaction. If any of those keys change prior the execution of that transaction, the entire transaction will be canceled and a WatchError will be raised. To implement our own client-side INCR command, we could do something like this:
WATCH命令。WATCH可以监视一个或多个key在开启事务之前。如果被监视的key在事务执行之前被修改过了,接下来的事务操作会被拒绝执行。实现我们自己客户端的INCR命令,可以像下面这样:
>>> with r.pipeline() as pipe:
... while 1:
... try:
... pipe.watch('OUR-SEQUENCE-KEY') #监视的key
... current_value = pipe.get('OUR-SEQUENCE-KEY')#获取存在的key值
... next_value = int(current_value) + 1#计算
... pipe.multi()#开启事务
... pipe.set('OUR-SEQUENCE-KEY', next_value)#设置key值
... pipe.execute()#执行
.... break#执行成功退出循环
... except WatchError:
.... continue#如果监视的值被别的客户端动了,会引发错误,但我们还得继续执行
#因为我们还没让key的值改变,直到修改了key的值,才结束
Note that, because the Pipeline must bind to a single connection for the duration of a WATCH, care must be taken to ensure that the connection is returned to the connection pool by calling the reset() method. If the Pipeline is used as a context manager (as in the example above) reset() will be called automatically. Of course you can do this the manual way by explicity calling reset():
注意:因为管道在执行WATCH期间绑定到一个连接,通过调用reset()方法确保连接被放回到连接池。
如果管道只作为命令缓冲,reset方法会自动被调用,你可以手动调用复位。
>>> pipe = r.pipeline()
>>> while 1:
... try:
... pipe.watch('OUR-SEQUENCE-KEY')
...
... pipe.execute()
... break
... except WatchError:
... continue
... finally:
... pipe.reset()
A convenience method named "transaction" exists for handling all the boilerplate of handling and retrying watch errors. It takes a callable that should expect a single parameter, a pipeline object, and any number of keys to be WATCHed. Our client-side INCR command above can be written like this, which is much easier to read:
存在一个名为transaction的便利方法,可以处理上面的情况,尝试下看看情况。
有一个可调用参数-管道对象,或者其他的需要WATCH监视的key.
我们的客户端INCR命令可以写成下面这样,更简单和易读。
>>> def client_side_incr(pipe):
... current_value = pipe.get('OUR-SEQUENCE-KEY')
... next_value = int(current_value) + 1
... pipe.multi()
... pipe.set('OUR-SEQUENCE-KEY', next_value)
>>>
>>> r.transaction(client_side_incr, 'OUR-SEQUENCE-KEY')
[True]