色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術文章
文章詳情頁

用python 批量操作redis數據庫

瀏覽:2日期:2022-06-25 16:44:14
方法一:使用 pipeline

使用pipelining 發送命令時,redis server必須部分請求放到隊列中(使用內存)執行完畢后一次性發送結果,在 pipeline 使用期間,將“獨占”鏈接,無法進行非“管道”類型的其他操作,直至 pipeline 關閉;如果 pipeline 的指令集很多很龐大,為了不影響其他操作(redis 最大時間lua-time-limit默認是5s),可以使用其他新建新鏈接操作。批量操作如下:

import redisr = redis.Redis(host=’127.0.0.1’, port=6379, password=’1234567890’)with r.pipeline() as ctx: a = time.time() ctx.hset(’current’, 'time2', a) ctx.hset(’current’, 'time3', a) res = ctx.execute() print('result: ', res)

使用 pipe line 以樂觀鎖的形式執行事務操作

# -*- coding:utf-8 -*-import redisfrom redis import WatchErrorfrom concurrent.futures import ProcessPoolExecutorr = redis.Redis(host=’127.0.0.1’, port=6379)# 減庫存函數, 循環直到減庫存完成# 庫存充足, 減庫存成功, 返回True# 庫存不足, 減庫存失敗, 返回Falsedef 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() # multi 判斷 watch 監控的 key 是否被其他客戶端改變 pipe.decr(’stock:count’) # 把命令推送過去 # execute返回命令執行結果列表, 這里只有一個decr返回當前值 result = pipe.execute()[0] print('result: ', result) return Trueelse: return False except WatchError as e:# 打印WatchError異常, 觀察被watch鎖住的情況print(e.args) finally:pipe.unwatch()def worker(): while True: # 沒有庫存就退出 if not decr_stock(): break# 實驗開始# 設置庫存為100r.set('stock:count', 100)# 多進程模擬多個客戶端提交with ProcessPoolExecutor(max_workers=2) as pool: for _ in range(10): pool.submit(worker)方法二:使用 register_script

分布執行,發送腳本到redis服務器,獲取一個本次連接的一個調用句柄,根據此句柄可以無數次執行不同參數調用

import redisimport time r = redis.Redis(host=’127.0.0.1’, port=31320, password=’12345678’) lua = ''' local key = KEYS[1] local field = ARGV[1] local timestamp_new = ARGV[2] -- get timestamp of the key in redis local timestamp_old = redis.call(’hget’, key, field) -- if timestamp_old == nil, it means the key is not exist if timestamp_old == nil or timestamp_old == false or timestamp_new > timestamp_old then redis.call(’hset’, key, field .. 1, timestamp_new) -- timestamp_new > timestamp_old return redis.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 客戶端。

import redisimport time r = redis.Redis(host=’127.0.0.1’, port=31320, password=’12345678’) lua = ''' local key = KEYS[1] local field = ARGV[1] local timestamp_new = ARGV[2] -- get timestamp of the key in redis local timestamp_old = redis.call(’hget’, key, field) -- if timestamp_old == nil, it means the key is not exist if timestamp_old == nil or timestamp_old == false or timestamp_new > timestamp_old then redis.call(’hset’, key, field .. 1, timestamp_new) -- timestamp_new > timestamp_old return redis.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_... )

script load

此命令用于將Lua腳本加載到Redis內存中

script exists

scripts exists sha1 [sha1 …] 此命令用于判斷sha1是否已經加載到Redis內存中

script flush

此命令用于清除Redis內存已經加載的所有Lua腳本,在執行script flush后,所有 sha 不復存在。

script kill

此命令用于殺掉正在執行的Lua腳本。

方法四:eval

使用方法與方法三類似,但是eval是一次性請求,每次請求,必須攜帶 lua 腳本

以上就是用python 批量操作redis數據庫的詳細內容,更多關于python 批量操作redis數據庫的資料請關注好吧啦網其它相關文章!

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 日韩高清不卡在线 | 欧美在线成人免费国产 | 免费人成年短视频在线观看网站 | 久久久久久亚洲精品不卡 | 免费观看欧美精品成人毛片能看的 | jyzzjyzzjyzz日本在线观看 | 古代级a毛片在线 | 国产成人精品视频免费大全 | 台湾香港澳门三级在线 | 国产精品制服 | 91久久精品国产91久久性色tv | 亚洲一区二区三区影院 | 久久精品免费一区二区三区 | 欧美亚洲国产精品久久高清 | 欧美一级片播放 | 一级做a毛片免费视频 | 国产在线一二三区 | 欧美成人免费一区在线播放 | 久久精品视频一区二区三区 | 中文字幕亚洲精品久久 | 自拍偷拍视频在线观看 | 一级日韩一级欧美 | 一级毛片成人免费看免费不卡 | 美女黄频免费看 | 男女朋友做爽爽爽免费视频网 | 福利一二三区 | 亚洲欧美自拍视频 | 国产一区二区精品在线观看 | 亚洲怡红院在线 | 欧美手机在线视频 | 亚洲视频在线观看地址 | 97视频网站| 日韩一区视频在线 | 久久性生大片免费观看性 | 人成免费a级毛片 | 亚洲最黄视频 | 亚洲综合网址 | 114一级毛片免费观看 | 日鲁夜鲁鲁狠狠综合视频 | 91精品国产91久久久久 | 久久久国产精品免费 |