>>>>>>>>>>>>lock= r.lock('mylock_one', timeout=2)>>>>>>lock.acquire()True>>>lock.release()Traceback(most recent call last):File"<stdin>", line 1,in<module>File"/home/ljk/.virtualenvs/work/lib/python3.7/site-packages/redis/lock.py", line 232,in release
self.do_release(expected_token)File"/home/ljk/.virtualenvs/work/lib/python3.7/site-packages/redis/lock.py", line 238,in do_release
raiseLockNotOwnedError("Cannot release a lock"redis.exceptions.LockNotOwnedError:Cannot release a lock that's no longer owned
>>>
import threading
import time as mod_time
import uuid
from redis.exceptions importLockError,LockNotOwnedErrorfrom redis.utils import dummy
classLock(object):"""
A shared, distributed Lock. Using Redis for locking allows the Lock
to be shared across processes and/or machines.
It's left to the user to resolve deadlock issues and make sure
multiple clients play nicely together.
""" lua_release =None lua_extend =None lua_reacquire =None# KEYS[1] - lock name# ARGV[1] - token# return 1 if the lock was released, otherwise 0 LUA_RELEASE_SCRIPT ="""
local token = redis.call('get', KEYS[1])
if not token or token ~= ARGV[1] then
return 0
end
redis.call('del', KEYS[1])
return 1
"""# KEYS[1] - lock name# ARGV[1] - token# ARGV[2] - additional milliseconds# ARGV[3] - "0" if the additional time should be added to the lock's# existing ttl or "1" if the existing ttl should be replaced# return 1 if the locks time was extended, otherwise 0 LUA_EXTEND_SCRIPT ="""
local token = redis.call('get', KEYS[1])
if not token or token ~= ARGV[1] then
return 0
end
local expiration = redis.call('pttl', KEYS[1])
if not expiration then
expiration = 0
end
if expiration < 0 then
return 0
end
local newttl = ARGV[2]
if ARGV[3] == "0" then
newttl = ARGV[2] + expiration
end
redis.call('pexpire', KEYS[1], newttl)
return 1
"""# KEYS[1] - lock name# ARGV[1] - token# ARGV[2] - milliseconds# return 1 if the locks time was reacquired, otherwise 0 LUA_REACQUIRE_SCRIPT ="""
local token = redis.call('get', KEYS[1])
if not token or token ~= ARGV[1] then
return 0
end
redis.call('pexpire', KEYS[1], ARGV[2])
return 1
"""def __init__(self, redis, name, timeout=None, sleep=0.1, blocking=True, blocking_timeout=None, thread_local=True):"""
Create a new Lock instance named ``name`` using the Redis client
supplied by ``redis``.
``timeout`` indicates a maximum life for the lock.
By default, it will remain locked until release() is called.
``timeout`` can be specified as a float or integer, both representing
the number of seconds to wait.
``sleep`` indicates the amount of time to sleep per loop iteration
when the lock is in blocking mode and another client is currently
holding the lock.
``blocking`` indicates whether calling ``acquire`` should block until
the lock has been acquired or to fail immediately, causing ``acquire``
to return False and the lock not being acquired. Defaults to True.
Note this value can be overridden by passing a ``blocking``
argument to ``acquire``.
``blocking_timeout`` indicates the maximum amount of time in seconds to
spend trying to acquire the lock. A value of ``None`` indicates
continue trying forever. ``blocking_timeout`` can be specified as a
float or integer, both representing the number of seconds to wait.
``thread_local`` indicates whether the lock token is placed in
thread-local storage. By default, the token is placed in thread local
storage so that a thread only sees its token, not a token set by
another thread. Consider the following timeline:
time: 0, thread-1 acquires `my-lock`, with a timeout of 5 seconds.
thread-1 sets the token to "abc"
time: 1, thread-2 blocks trying to acquire `my-lock` using the
Lock instance.
time: 5, thread-1 has not yet completed. redis expires the lock
key.
time: 5, thread-2 acquired `my-lock` now that it's available.
thread-2 sets the token to "xyz"
time: 6, thread-1 finishes its work and calls release(). if the
token is *not* stored in thread local storage, then
thread-1 would see the token value as "xyz" and would be
able to successfully release the thread-2's lock.
In some use cases it's necessary to disable thread local storage. For
example, if you have code where one thread acquires a lock and passes
that lock instance to a worker thread to release later. If thread
local storage isn't disabled in this case, the worker thread won't see
the token set by the thread that acquired the lock. Our assumption
is that these cases aren't common and as such default to using
thread local storage.
"""self.redis = redis
self.name = name
self.timeout = timeout
self.sleep = sleep
self.blocking = blocking
self.blocking_timeout = blocking_timeout
self.thread_local =bool(thread_local)self.local= threading.local()ifself.thread_local else dummy()self.local.token =Noneself.register_scripts()def register_scripts(self): cls =self.__class__
client =self.redis
if cls.lua_release isNone: cls.lua_release = client.register_script(cls.LUA_RELEASE_SCRIPT)if cls.lua_extend isNone: cls.lua_extend = client.register_script(cls.LUA_EXTEND_SCRIPT)if cls.lua_reacquire isNone: cls.lua_reacquire = \
client.register_script(cls.LUA_REACQUIRE_SCRIPT)def __enter__(self):# force blocking, as otherwise the user would have to check whether# the lock was actually acquired or not.ifself.acquire(blocking=True):returnselfraiseLockError("Unable to acquire lock within the time specified")def __exit__(self, exc_type, exc_value, traceback):self.release()def acquire(self, blocking=None, blocking_timeout=None, token=None):"""
Use Redis to hold a shared, distributed lock named ``name``.
Returns True once the lock is acquired.
If ``blocking`` is False, always return immediately. If the lock
was acquired, return True, otherwise return False.
``blocking_timeout`` specifies the maximum number of seconds to
wait trying to acquire the lock.
``token`` specifies the token value to be used. If provided, token
must be a bytes object or a string that can be encoded to a bytes
object with the default encoding. If a token isn't specified, a UUID
will be generated.
""" sleep =self.sleep
if token isNone: token = uuid.uuid1().hex.encode()else: encoder =self.redis.connection_pool.get_encoder() token = encoder.encode(token)if blocking isNone: blocking =self.blocking
if blocking_timeout isNone: blocking_timeout =self.blocking_timeout
stop_trying_at =Noneif blocking_timeout isnotNone: stop_trying_at = mod_time.time()+ blocking_timeout
whileTrue:ifself.do_acquire(token):self.local.token = token
returnTrueifnot blocking:returnFalse next_try_at = mod_time.time()+ sleep
if stop_trying_at isnotNoneand next_try_at > stop_trying_at:returnFalse mod_time.sleep(sleep)def do_acquire(self, token):ifself.timeout:# convert to milliseconds timeout =int(self.timeout *1000)else: timeout =Noneifself.redis.set(self.name, token, nx=True, px=timeout):returnTruereturnFalsedef locked(self):"""
Returns True if this key is locked by any process, otherwise False.
"""returnself.redis.get(self.name)isnotNonedef owned(self):"""
Returns True if this key is locked by this lock, otherwise False.
""" stored_token =self.redis.get(self.name)# need to always compare bytes to bytes# TODO: this can be simplified when the context manager is finishedif stored_token andnot isinstance(stored_token, bytes): encoder =self.redis.connection_pool.get_encoder() stored_token = encoder.encode(stored_token)returnself.local.token isnotNoneand \
stored_token ==self.local.token
def release(self):"Releases the already acquired lock" expected_token =self.local.token
if expected_token isNone:raiseLockError("Cannot release an unlocked lock")self.local.token =Noneself.do_release(expected_token)def do_release(self, expected_token):ifnotbool(self.lua_release(keys=[self.name], args=[expected_token], client=self.redis)):raiseLockNotOwnedError("Cannot release a lock"" that's no longer owned")def extend(self, additional_time, replace_ttl=False):"""
Adds more time to an already acquired lock.
``additional_time`` can be specified as an integer or a float, both
representing the number of seconds to add.
``replace_ttl`` if False (the default), add `additional_time` to
the lock's existing ttl. If True, replace the lock's ttl with
`additional_time`.
"""ifself.local.token isNone:raiseLockError("Cannot extend an unlocked lock")ifself.timeout isNone:raiseLockError("Cannot extend a lock with no timeout")returnself.do_extend(additional_time, replace_ttl)def do_extend(self, additional_time, replace_ttl): additional_time =int(additional_time *1000)ifnotbool(self.lua_extend( keys=[self.name], args=[self.local.token, additional_time, replace_ttl and"1"or"0"], client=self.redis,)):raiseLockNotOwnedError("Cannot extend a lock that's"" no longer owned")returnTruedef reacquire(self):"""
Resets a TTL of an already acquired lock back to a timeout value.
"""ifself.local.token isNone:raiseLockError("Cannot reacquire an unlocked lock")ifself.timeout isNone:raiseLockError("Cannot reacquire a lock with no timeout")returnself.do_reacquire()def do_reacquire(self): timeout =int(self.timeout *1000)ifnotbool(self.lua_reacquire(keys=[self.name], args=[self.local.token, timeout], client=self.redis)):raiseLockNotOwnedError("Cannot reacquire a lock that's"" no longer owned")returnTrue
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理