summaryrefslogtreecommitdiff
path: root/redis/lock.py
diff options
context:
space:
mode:
authorAnas <anas.el.amraoui@live.com>2021-11-30 18:05:51 +0200
committerGitHub <noreply@github.com>2021-11-30 18:05:51 +0200
commitb94e230b17d08e6c89d134e933c706256b79bc4a (patch)
tree993bd7565169229326b810b66939587431ab9dc6 /redis/lock.py
parent368a25f9d163d784a8896f1c087582405e98e006 (diff)
downloadredis-py-b94e230b17d08e6c89d134e933c706256b79bc4a.tar.gz
Added black and isort (#1734)
Diffstat (limited to 'redis/lock.py')
-rw-r--r--redis/lock.py55
1 files changed, 26 insertions, 29 deletions
diff --git a/redis/lock.py b/redis/lock.py
index d229752..95bb413 100644
--- a/redis/lock.py
+++ b/redis/lock.py
@@ -2,6 +2,7 @@ import threading
import time as mod_time
import uuid
from types import SimpleNamespace
+
from redis.exceptions import LockError, LockNotOwnedError
@@ -70,8 +71,16 @@ class Lock:
return 1
"""
- def __init__(self, redis, name, timeout=None, sleep=0.1,
- blocking=True, blocking_timeout=None, thread_local=True):
+ 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``.
@@ -129,11 +138,7 @@ class Lock:
self.blocking = blocking
self.blocking_timeout = blocking_timeout
self.thread_local = bool(thread_local)
- self.local = (
- threading.local()
- if self.thread_local
- else SimpleNamespace()
- )
+ self.local = threading.local() if self.thread_local else SimpleNamespace()
self.local.token = None
self.register_scripts()
@@ -145,8 +150,7 @@ class Lock:
if cls.lua_extend is None:
cls.lua_extend = client.register_script(cls.LUA_EXTEND_SCRIPT)
if cls.lua_reacquire is None:
- cls.lua_reacquire = \
- client.register_script(cls.LUA_REACQUIRE_SCRIPT)
+ cls.lua_reacquire = client.register_script(cls.LUA_REACQUIRE_SCRIPT)
def __enter__(self):
if self.acquire():
@@ -222,8 +226,7 @@ class Lock:
if stored_token and not isinstance(stored_token, bytes):
encoder = self.redis.connection_pool.get_encoder()
stored_token = encoder.encode(stored_token)
- return self.local.token is not None and \
- stored_token == self.local.token
+ return self.local.token is not None and stored_token == self.local.token
def release(self):
"Releases the already acquired lock"
@@ -234,11 +237,10 @@ class Lock:
self.do_release(expected_token)
def do_release(self, expected_token):
- if not bool(self.lua_release(keys=[self.name],
- args=[expected_token],
- client=self.redis)):
- raise LockNotOwnedError("Cannot release a lock"
- " that's no longer owned")
+ if not bool(
+ self.lua_release(keys=[self.name], args=[expected_token], client=self.redis)
+ ):
+ raise LockNotOwnedError("Cannot release a lock" " that's no longer owned")
def extend(self, additional_time, replace_ttl=False):
"""
@@ -262,17 +264,11 @@ class Lock:
if not bool(
self.lua_extend(
keys=[self.name],
- args=[
- self.local.token,
- additional_time,
- replace_ttl and "1" or "0"
- ],
+ args=[self.local.token, additional_time, replace_ttl and "1" or "0"],
client=self.redis,
)
):
- raise LockNotOwnedError(
- "Cannot extend a lock that's" " no longer owned"
- )
+ raise LockNotOwnedError("Cannot extend a lock that's" " no longer owned")
return True
def reacquire(self):
@@ -287,9 +283,10 @@ class Lock:
def do_reacquire(self):
timeout = int(self.timeout * 1000)
- if not bool(self.lua_reacquire(keys=[self.name],
- args=[self.local.token, timeout],
- client=self.redis)):
- raise LockNotOwnedError("Cannot reacquire a lock that's"
- " no longer owned")
+ if not bool(
+ self.lua_reacquire(
+ keys=[self.name], args=[self.local.token, timeout], client=self.redis
+ )
+ ):
+ raise LockNotOwnedError("Cannot reacquire a lock that's" " no longer owned")
return True