#!/usr/bin/env python
# coding=utf-8 import time
import redis class RedisLock(object):
def __init__(self, key):
self.rdcon = redis.Redis(host='', port=6379, password="", db=1)
self._lock = 0
self.lock_key = "%s_dynamic_test" % key @staticmethod
def get_lock(cls, timeout=10):
while cls._lock != 1:
timestamp = time.time() + timeout + 1
cls._lock = cls.rdcon.setnx(cls.lock_key, timestamp)
       # 注意下方括号的范围
if cls._lock == 1 or (time.time() > cls.rdcon.get(cls.lock_key) and time.time() > cls.rdcon.getset(cls.lock_key, timestamp)):
print "get lock"
break
else:
time.sleep(0.3) @staticmethod
def release(cls):
if time.time() < cls.rdcon.get(cls.lock_key):
print "release lock"
cls.rdcon.delete(cls.lock_key) def deco(cls):
def _deco(func):
def __deco(*args, **kwargs):
print "before %s called [%s]."%(func.__name__, cls)
cls.get_lock(cls)
try:
return func(*args, **kwargs)
finally:
cls.release(cls)
return __deco
return _deco @deco(RedisLock(""))
def myfunc():
print "myfunc() called."
time.sleep(20) if __name__ == "__main__":
myfunc()

eg2:

 import redis
import time, datetime def acquire_lock(conn, lockname, identifier, expire=10):
if conn.setnx(lockname, identifier):
conn.expire(lockname, expire)
return identifier
elif not conn.ttl(lockname):
conn.expire(lockname, expire) return False def release_lock(conn, lockname, identifier):
pipe = conn.pipeline(True)
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 # we lost the lock
return False conn = redis.Redis(host='localhost', port=6379, db=0) # 1 identifier
# 2 False
# 11 True
# 22 False
# 33 barx2
# 44 True ret = acquire_lock(conn, "lockname", "identifier", 3)
print "", ret
ret = acquire_lock(conn, "lockname", "identifier", 3)
print "", ret
ret = release_lock(conn, "lockname", "identifier")
print "", ret
ret = release_lock(conn, "lockname", "identifier")
print "", ret ret = acquire_lock(conn, "footest", "bartest", 10)
print "", ret

最新文章

  1. C Primer Plus_第6章_循环_编程练习
  2. java基础-继承
  3. First class ,6 examples anlaysisi
  4. C语言实现Web客户端(转-kungstriving)
  5. IIS网站服务器性能优化指南(转载)
  6. Apache OFBiz 学习笔记 之 服务引擎 一
  7. 【转】Ubuntu乱码解决方案(全)
  8. .net中的Array,ArrayList和List
  9. web前端-----jQuery
  10. JavaScript push() 方法
  11. [python]使用django快速生成自己的博客小站,含详细部署方法
  12. Iroha and a Grid AtCoder - 1974(思维水题)
  13. Linux基础命令---lpstat查看打印任务
  14. VirtualBox 安装Mac OS
  15. (string高精度)A + B Problem II hdu1002
  16. loj#6491. zrq 学反演
  17. laravel扩展推荐
  18. 11.5 Daily Scrum
  19. Jekins - Hello world,Jekins + Maven + Git + Tomcat 的简单应用
  20. C#编程(七)----------命名空间

热门文章

  1. win10提示“无法设置移动热点 请打开WLAN”的解决方法
  2. TreeMap实现
  3. 深度学习论文翻译解析(八):Rich feature hierarchies for accurate object detection and semantic segmentation
  4. Spring Cloud Alibaba基础教程:Nacos 生产级版本 0.8.0
  5. 再探JVM内存模型
  6. NLP(一)
  7. 解决只能通过localhost访问Elasticsearch的问题
  8. C++中复杂声明和定义的辨析
  9. WPF DataGrid ScrollBar Style
  10. day71 django收尾