日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

python 分布式锁_python分布式锁

發(fā)布時(shí)間:2025/3/13 python 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python 分布式锁_python分布式锁 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

在進(jìn)行某些比較耗時(shí)的查詢時(shí),為了避免進(jìn)行重復(fù)計(jì)算,可以采用分布式鎖服務(wù),

在同一個(gè)時(shí)間只有一個(gè)操作在進(jìn)行,同類的操作進(jìn)行等待重試.

下面的代碼(fetch_with_dist_lock)定義了一個(gè)fetcher,一個(gè)updater.

如果fetcher獲取不到數(shù)據(jù),則使用updater進(jìn)行更新.更新成功之后通過fetcher返回結(jié)果.

也有一些情況,我們只想更新某個(gè)數(shù)據(jù),更新者是多個(gè),但是更新操作不是原子的.那么

我們會(huì)通過update_with_dist_lock來進(jìn)行. def fetch_with_dist_lock(mc_store, mutex_key, fetcher, updater,

lock_time=3*60*1000, sleep_time=100, retry_times=3):

i = 0

while i < retry_times:

i += 1

need_update, results = fetcher()

if need_update:

if(mc_store.add(mutex_key, lock_time)):

try:

updater()

continue

finally:

#release the distribute mutex anyway

mc_store.delete(mutex_key)

else:

time.sleep((sleep_time*1.0)/1000)

continue

return results

#too much tries, but failed still.

return None

def f_wrapper(f, *args, **kwargs):

def _():

return f(*args, **kwargs)

return _

def update_with_dist_lock(mc_store, mutex_key, updater, lock_time=60*1000, sleep_time=100, retry_times=5):

i = 0

while i < retry_times:

i += 1

if (mc_store.add(mutex_key, lock_time)):

try:

updater()

return True

finally:

mc_store.delete(mutex_key)

else:

time.sleep((sleep_time*1.0)/1000)

continue

return False

本文原創(chuàng)發(fā)布php中文網(wǎng),轉(zhuǎn)載請注明出處,感謝您的尊重!

總結(jié)

以上是生活随笔為你收集整理的python 分布式锁_python分布式锁的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。