# redis pieline

redis pieline


1. 简介

Redis是建立在TCP协议基础上的CS架构,客户端client对redis server采取请求响应的方式交互。
一般来说客户端从提交请求到得到服务器相应,需要传送两个tcp报文。

对于同一客户端的大量命令而言,网络开销可能成为主要的时间开销。
类似于myslq,redis支持管道(pipeline),它可以一次性发送多条命令并在执行完后一次性将结果返回,减少客户端与redis的通信次数来实现降低往返延时时间,而且 Pipeline 实现的原理是队列,而队列的原理是时先进先出,这样就保证数据的顺序性。

1.1. 案例

  
import redis  
from redis import WatchError  
from concurrent.futures import ProcessPoolExecutor  

r = redis.Redis(host='127.0.0.1', port=6379)  

# 减库存函数, 循环直到减库存完成  
# 库存充足, 减库存成功, 返回True  
# 库存不足, 减库存失败, 返回False  
def decr_stock():  

    # python中redis事务是通过pipeline的封装实现的  
    with r.pipeline() as pipe:  
        while True:  
            try:  
                # watch库存键, multi后如果该key被其他客户端改变, 事务操作会抛出WatchError异常  
                pipe.watch('stock:count')  
                count = int(pipe.get('stock:count'))  
                if count > 0:  # 有库存  
                    # 事务开始  
                    pipe.multi()  
                    pipe.decr('stock:count')  
                    # 把命令推送过去  
                    # execute返回命令执行结果列表, 这里只有一个decr返回当前值  
                    print pipe.execute()[0]  
                    return True  
                else:  
                    return False  
            except WatchError, ex:  
                # 打印WatchError异常, 观察被watch锁住的情况  
                print ex  
                pipe.unwatch()  

def worker():  
    while True:  
        # 没有库存就退出  
        if not decr_stock():  
            break  


# 实验开始  
# 设置库存为100  
r.set("stock:count", 100)  

# 多进程模拟多个客户端提交  
with ProcessPoolExecutor(max_workers=2) as pool:  
    for _ in range(10):  
        pool.submit(worker)  

猜你喜欢

转载自www.cnblogs.com/wodeboke-y/p/12897677.html