Redis-Lock 使用指南
import redis
import time
import uuid
class RedisLock(object):
def __init__(self, client, lock_name, acquire_timeout=10, lock_timeout=10):
self.client = client
self.lock_name = lock_name
self.acquire_timeout = acquire_timeout
self.lock_timeout = lock_timeout
self.uuid = str(uuid.uuid4())
self.lock_key = 'lock:' + lock_name
self.uuid_key = 'lock_uuid:' + lock_name
def _acquire(self):
end = time.time() + self.acquire_timeout
while time.time() < end:
self.client.set(self.lock_key, '1', ex=self.lock_timeout, nx=True)
self.client.set(self.uuid_key, self.uuid)
if self.client.get(self.lock_key) == '1' and self.uuid == self.client.get(self.uuid_key):
return True
time.sleep(0.001)
return False
def acquire(self):
return self._acquire()
def release(self):
if self.client.get(self.lock_key) == '1' and self.uuid == self.client.get(self.uuid_key):
self.client.delete(self.lock_key)
self.client.delete(self.uuid_key)
return True
return False
# 使用示例
client = redis.StrictRedis(host='localhost', port=6379, db=0)
lock = RedisLock(client, 'my_lock')
if lock.acquire():
try:
# 这里放置需要互斥执行的代码
print('Lock acquired. Exclusive access to the code block.')
finally:
# 确保释放锁
if not lock.release():
print('Unable to release the lock!')
else:
print('Unable to acquire the lock.')
这段代码实现了一个基本的Redis分布式锁。它使用Redis的SET命令的NX
(只在键不存在时设置)和PX
(设置键的过期时间)选项来尝试获取锁。在释放锁时,它检查锁是否仍然属于请求者,然后删除键以释放锁。这个实现没有使用看门狗机制,因此在获取锁后,如果执行的时间超过了锁的超时时间,锁会自动失效,避免了死锁的发生。
评论已关闭