第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

關(guān)于《redis實戰(zhàn)》中6-9代碼片段使用setex鎖的不解

關(guān)于《redis實戰(zhàn)》中6-9代碼片段使用setex鎖的不解

眼眸繁星 2019-02-20 21:16:36
想了很久,實在不能理解代碼清單6-9中鎖的使用原理,鎖只是設(shè)置了一個key,而且完全沒有用到(比如if getkey()==true之類的),生成的鎖還是隨機的,也就是不還是沒解決并發(fā)的問題嗎? 同一時間仍然有可能會有其他客戶端對數(shù)據(jù)進行操作。求大佬解惑,謝謝。 # 代碼清單 6-8 # <start id="_1314_14473_8641"/> def acquire_lock(conn, lockname, acquire_timeout=10): # 128位隨機標識符。 identifier = str(uuid.uuid4()) end = time.time() + acquire_timeout while time.time() < end: # 嘗試取得鎖。 if conn.setnx('lock:' + lockname, identifier): return identifier time.sleep(.001) return False # <end id="_1314_14473_8641"/> # 代碼清單 6-9 # <start id="_1314_14473_8645"/> def purchase_item_with_lock(conn, buyerid, itemid, sellerid): buyer = "users:%s" % buyerid seller = "users:%s" % sellerid item = "%s.%s" % (itemid, sellerid) inventory = "inventory:%s" % buyerid # 嘗試獲取鎖。 locked = acquire_lock(conn, 'market:') if not locked: return False pipe = conn.pipeline(True) try: # 檢查物品是否已經(jīng)售出,以及買家是否有足夠的金錢來購買物品。 pipe.zscore("market:", item) pipe.hget(buyer, 'funds') price, funds = pipe.execute() if price is None or price > funds: return None # 將買家支付的貨款轉(zhuǎn)移給賣家,并將售出的物品轉(zhuǎn)移給買家。 pipe.hincrby(seller, 'funds', int(price)) pipe.hincrby(buyer, 'funds', int(-price)) pipe.sadd(inventory, itemid) pipe.zrem("market:", item) pipe.execute() return True finally: # 釋放鎖。 release_lock(conn, 'market:', locked) # <end id="_1314_14473_8645"/> # 代碼清單 6-10 # <start id="_1314_14473_8650"/> def release_lock(conn, lockname, identifier): pipe = conn.pipeline(True) lockname = 'lock:' + lockname while True: try: # 檢查并確認進程還持有著鎖。 pipe.watch(lockname) if pipe.get(lockname) == identifier: # 釋放鎖。 pipe.multi() pipe.delete(lockname) pipe.execute() return True pipe.unwatch() break # 有其他客戶端修改了鎖;重試。 except redis.exceptions.WatchError: pass # 進程已經(jīng)失去了鎖。 return False # <end id="_1314_14473_8650"/>
查看完整描述

2 回答

?
慕斯709654

TA貢獻1840條經(jīng)驗 獲得超5個贊

請仔細查看一下手冊,setnx命令是如果當前redis中沒有設(shè)置的key才會設(shè)置成功返回1,否則設(shè)置失敗返回0,把它當成鎖使用,就是利用了這個特性,至于設(shè)置什么值關(guān)系不大。

查看完整回答
反對 回復(fù) 2019-03-01
?
鴻蒙傳說

TA貢獻1865條經(jīng)驗 獲得超7個贊

單從例子上看,acquire_lock 中使用了setnx,只有一個操作可以鎖定成功。而且redis是單進程的,所有命令都是一個接著一個串行處理。所以不覺得有什么并發(fā)問題

查看完整回答
反對 回復(fù) 2019-03-01
  • 2 回答
  • 0 關(guān)注
  • 760 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號