diff options
author | Sibuken <zeden123@gmail.com> | 2022-12-01 17:33:47 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-12-01 16:33:47 +0200 |
commit | c48dc8310717344374db6a31000e92cfe1ae35f8 (patch) | |
tree | 06d500960cd605fe6ae5b30b6f8aac5bc797e52b | |
parent | 2c121552faf0d39267969b62ce0c3276391b37cc (diff) | |
download | redis-py-c48dc8310717344374db6a31000e92cfe1ae35f8.tar.gz |
Async: added 'blocking' argument to call lock method (#2454)
Co-authored-by: Chayim <chayim@users.noreply.github.com>
-rw-r--r-- | redis/asyncio/client.py | 8 | ||||
-rw-r--r-- | redis/asyncio/cluster.py | 8 | ||||
-rw-r--r-- | tests/test_asyncio/test_lock.py | 8 |
3 files changed, 24 insertions, 0 deletions
diff --git a/redis/asyncio/client.py b/redis/asyncio/client.py index e0ed85e..abe7d67 100644 --- a/redis/asyncio/client.py +++ b/redis/asyncio/client.py @@ -354,6 +354,7 @@ class Redis( name: KeyT, timeout: Optional[float] = None, sleep: float = 0.1, + blocking: bool = True, blocking_timeout: Optional[float] = None, lock_class: Optional[Type[Lock]] = None, thread_local: bool = True, @@ -369,6 +370,12 @@ class Redis( 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 @@ -411,6 +418,7 @@ class Redis( name, timeout=timeout, sleep=sleep, + blocking=blocking, blocking_timeout=blocking_timeout, thread_local=thread_local, ) diff --git a/redis/asyncio/cluster.py b/redis/asyncio/cluster.py index e0e77c7..ac61314 100644 --- a/redis/asyncio/cluster.py +++ b/redis/asyncio/cluster.py @@ -801,6 +801,7 @@ class RedisCluster(AbstractRedis, AbstractRedisCluster, AsyncRedisClusterCommand name: KeyT, timeout: Optional[float] = None, sleep: float = 0.1, + blocking: bool = True, blocking_timeout: Optional[float] = None, lock_class: Optional[Type[Lock]] = None, thread_local: bool = True, @@ -816,6 +817,12 @@ class RedisCluster(AbstractRedis, AbstractRedisCluster, AsyncRedisClusterCommand 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 @@ -858,6 +865,7 @@ class RedisCluster(AbstractRedis, AbstractRedisCluster, AsyncRedisClusterCommand name, timeout=timeout, sleep=sleep, + blocking=blocking, blocking_timeout=blocking_timeout, thread_local=thread_local, ) diff --git a/tests/test_asyncio/test_lock.py b/tests/test_asyncio/test_lock.py index 56387fa..15c3ec5 100644 --- a/tests/test_asyncio/test_lock.py +++ b/tests/test_asyncio/test_lock.py @@ -97,6 +97,14 @@ class TestLock: assert 8 < (await r.pttl("foo")) <= 9500 await lock.release() + async def test_blocking(self, r): + blocking = False + lock = self.get_lock(r, "foo", blocking=blocking) + assert not lock.blocking + + lock_2 = self.get_lock(r, "foo") + assert lock_2.blocking + async def test_blocking_timeout(self, r, event_loop): lock1 = self.get_lock(r, "foo") assert await lock1.acquire(blocking=False) |