* 用with做鎖的邏輯語(yǔ)句
* timeout避免了避免了任務(wù)出現(xiàn)異常,沒(méi)有做delete操作
* 對(duì)于長(zhǎng)時(shí)間的業(yè)務(wù),增加retry重試鎖的次數(shù)
#coding:utf-8 #my blog: http://xiaorui.cc from __future__ import with_statement import redis import time from redis_netlock import dist_lock client = redis.Redis(connection_pool=redis.BlockingConnectionPool(max_connections=15, host='localhost', port=6379)) with dist_lock('test', client): time.sleep(10) print 'welcome to my blog, http://xiaorui.cc'
同時(shí)運(yùn)行test.py and test2.py
python test.py
root@li568-252 redis_netlock]# python test.py welcome to my blog, http://xiaorui.cc
python test2.py
[root@li568-252 redis_netlock]# python test2.py Sleeipng for 0.404031367603 Sleeipng for 1.19347232007 Sleeipng for 2.47668271021 Sleeipng for 4.55280708824 hello
已經(jīng)把redis_netlock提交到了pypi項(xiàng)目里面。
pip install redis_netlock
下面是redis_netlock的git地址。
git clone https://github.com/rfyiamcool/redis_netlock.git
原文地址:python使用redis實(shí)現(xiàn)協(xié)同控制的分布式鎖, 感謝原作者分享。
聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問(wèn)題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com