使用pipeline以乐观锁的形式执行事务操作

#-*-coding:utf-8-*-

importredis
fromredisimportWatchError
fromconcurrent.futuresimportProcessPoolExecutor

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

#减库存函数,循环直到减库存完成
#库存充足,减库存成功,返回True
#库存不足,减库存失败,返回False
defdecr_stock():
#python中redis事务是通过pipeline的封装实现的
withr.pipeline()aspipe:
whileTrue:
try:
#watch库存键,multi后如果该key被其他客户端改变,事务操作会抛出WatchError异常
pipe.watch('stock:count')
count=int(pipe.get('stock:count'))
ifcount>0:#有库存
#事务开始
pipe.multi()#multi判断watch监控的key是否被其他客户端改变
pipe.decr('stock:count')
#把命令推送过去
#execute返回命令执行结果列表,这里只有一个decr返回当前值
result=pipe.execute()[0]
print("result:",result)
returnTrue
else:
returnFalse
exceptWatchErrorase:
#打印WatchError异常,观察被watch锁住的情况
print(e.args)
finally:
pipe.unwatch()


defworker():
whileTrue:
#没有库存就退出
ifnotdecr_stock():
break


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

#多进程模拟多个客户端提交
withProcessPoolExecutor(max_workers=2)aspool:
for_inrange(10):
pool.submit(worker)

方法二:使用 register_script

分布执行,发送脚本到redis服务器,获取一个本次连接的一个调用句柄,根据此句柄可以无数次执行不同参数调用

importredis
importtime

r=redis.Redis(host='127.0.0.1',port=31320,password='12345678')

lua="""
localkey=KEYS[1]
localfield=ARGV[1]
localtimestamp_new=ARGV[2]

--gettimestampofthekeyinredis
localtimestamp_old=redis.call('hget',key,field)
--iftimestamp_old==nil,itmeansthekeyisnotexist
iftimestamp_old==nilortimestamp_old==falseortimestamp_new>timestamp_oldthen
redis.call('hset',key,field..1,timestamp_new)
--timestamp_new>timestamp_old
returnredis.pcall('hset',key,field,timestamp_new)
end

"""

cmd=r.register_script(lua)

cur_time=time.time()
cmd(keys=['current'],args=["time",cur_time])

register_script调用lua来实现,需要注意 redis.call(method,key,field)的返回值(nil,false,1),此处没有键值返回的是false。如果中间有错误,所有的语句不时不生效。

方法三:使用script_load和evalsha

简而言之,通过script_load发送给redis服务器,使加载lua脚本,并常驻内存,返回标志,通过evalsha按标志进行执行,此连接脱离本次redis客户端。

importredis
importtime

r=redis.Redis(host='127.0.0.1',port=31320,password='12345678')

lua="""
localkey=KEYS[1]
localfield=ARGV[1]
localtimestamp_new=ARGV[2]

--gettimestampofthekeyinredis
localtimestamp_old=redis.call('hget',key,field)
--iftimestamp_old==nil,itmeansthekeyisnotexist
iftimestamp_old==nilortimestamp_old==falseortimestamp_new>timestamp_oldthen
redis.call('hset',key,field..1,timestamp_new)
--timestamp_new>timestamp_old
returnredis.pcall('hset',key,field,timestamp_new)
end

"""
sha=r.script_load(lua)
print(r.evalsha(sha,1,'current','time',time.time()))

Redis管理Lua脚本:(Python下为script_...)

此命令用于将Lua脚本加载到Redis内存中

scriptsexistssha1[sha1…]
此命令用于判断sha1是否已经加载到Redis内存中

此命令用于清除Redis内存已经加载的所有Lua脚本,在执行scriptflush后,所有sha不复存在。

此命令用于杀掉正在执行的Lua脚本。

方法四:eval

使用方法与方法三类似,但是eval是一次性请求,每次请求,必须携带lua脚本

以上就是用python批量操作redis数据库的详细内容,更多关于python批量操作redis数据库的资料请关注毛票票其它相关文章!

声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。

热门推荐

1 毛坯房验收经验和常识 看了之后再验房心里有底
2 二手房收房如何交接 二手房收房注意问题
3 专业验收毛坯房的价格 商品房验收合格的标准
4 精装房怎么验收 精装房请验房师有用吗
5 一般要到哪里找验房师 验房师有哪些作用呢
6 请人验房一般是多少钱 验房师费用是多少
7 怎样测量房子面积 建筑面积和使用面积怎么算
8 收房需要注意什么 仔细检查不松懈
9 收房时三书一证一表是什么 主要作用介绍
10 交房时交房税费有哪些 本文为你一一讲解
11 验房都需要验什么 要做哪些准备呢
12 毛坯房验房师有必要请吗 毛坯房装修完如何验收
13 地下室防水工程质量验收规范详解
14 水性涂料、油性涂料区别介绍
15 零基础布艺DIY工坊 教你做超萌猫头鹰钥匙包
16 三棵树漆怎么样?三棵树漆官方网站
17 家庭“装修套餐”中猫腻你知道吗?
18 小空间大浴望 卫浴间装修巧支招