From 5c851f1d28d37108908025a29017497950069414 Mon Sep 17 00:00:00 2001 From: Anas Date: Mon, 18 Apr 2022 11:21:54 +0300 Subject: [PATCH 1/3] Added blocking parameter to lock --- django_redis/client/default.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/django_redis/client/default.py b/django_redis/client/default.py index 1df90a27..ac01a531 100644 --- a/django_redis/client/default.py +++ b/django_redis/client/default.py @@ -338,9 +338,10 @@ def lock( self, key, version: Optional[int] = None, - timeout=None, - sleep=0.1, - blocking_timeout=None, + timeout: Optional[float] = None, + sleep: float = 0.1, + blocking: bool = True, + blocking_timeout: Optional[float] = None, client: Optional[Redis] = None, thread_local=True, ): @@ -352,6 +353,7 @@ def lock( key, timeout=timeout, sleep=sleep, + blocking=blocking, blocking_timeout=blocking_timeout, thread_local=thread_local, ) From 1b225fe4f9ddd89b3ef9005751cb06a700dcee99 Mon Sep 17 00:00:00 2001 From: WisdomPill Date: Sun, 20 Oct 2024 17:47:09 +0300 Subject: [PATCH 2/3] Added blocking parameter to lock --- django_redis/client/default.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/django_redis/client/default.py b/django_redis/client/default.py index b9a5c1b0..f6fd23bb 100644 --- a/django_redis/client/default.py +++ b/django_redis/client/default.py @@ -374,6 +374,7 @@ def lock( version: Optional[int] = None, timeout: Optional[float] = None, sleep: float = 0.1, + blocking: bool = True, blocking_timeout: Optional[float] = None, client: Optional[Redis] = None, thread_local: bool = True, @@ -386,6 +387,7 @@ def lock( key, timeout=timeout, sleep=sleep, + blocking=blocking, blocking_timeout=blocking_timeout, thread_local=thread_local, ) From 277f2eb07d7d49a895ac7b6de0ce4b2e5a483e55 Mon Sep 17 00:00:00 2001 From: WisdomPill Date: Sun, 20 Oct 2024 18:11:22 +0300 Subject: [PATCH 3/3] Added tests for not blocking lock timeout --- tests/test_backend.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/tests/test_backend.py b/tests/test_backend.py index 4ff60983..1714996a 100644 --- a/tests/test_backend.py +++ b/tests/test_backend.py @@ -677,7 +677,19 @@ def test_expire_at(self, cache: RedisCache): def test_lock(self, cache: RedisCache): lock = cache.lock("foobar") - lock.acquire(blocking=True) + assert lock.acquire(blocking=True) + + assert cache.has_key("foobar") + lock.release() + assert not cache.has_key("foobar") + + def test_lock_not_blocking(self, cache: RedisCache): + lock = cache.lock("foobar") + assert lock.acquire(blocking=False) + + lock2 = cache.lock("foobar") + + assert not lock2.acquire(blocking=False) assert cache.has_key("foobar") lock.release() @@ -685,7 +697,7 @@ def test_lock(self, cache: RedisCache): def test_lock_released_by_thread(self, cache: RedisCache): lock = cache.lock("foobar", thread_local=False) - lock.acquire(blocking=True) + assert lock.acquire(blocking=True) def release_lock(lock_): lock_.release()