Skip to content

Commit c48dc83

Browse files
Sibukenchayim
andauthored
Async: added 'blocking' argument to call lock method (#2454)
Co-authored-by: Chayim <chayim@users.noreply.github.com>
1 parent 2c12155 commit c48dc83

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

redis/asyncio/client.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,7 @@ def lock(
354354
name: KeyT,
355355
timeout: Optional[float] = None,
356356
sleep: float = 0.1,
357+
blocking: bool = True,
357358
blocking_timeout: Optional[float] = None,
358359
lock_class: Optional[Type[Lock]] = None,
359360
thread_local: bool = True,
@@ -369,6 +370,12 @@ def lock(
369370
when the lock is in blocking mode and another client is currently
370371
holding the lock.
371372
373+
``blocking`` indicates whether calling ``acquire`` should block until
374+
the lock has been acquired or to fail immediately, causing ``acquire``
375+
to return False and the lock not being acquired. Defaults to True.
376+
Note this value can be overridden by passing a ``blocking``
377+
argument to ``acquire``.
378+
372379
``blocking_timeout`` indicates the maximum amount of time in seconds to
373380
spend trying to acquire the lock. A value of ``None`` indicates
374381
continue trying forever. ``blocking_timeout`` can be specified as a
@@ -411,6 +418,7 @@ def lock(
411418
name,
412419
timeout=timeout,
413420
sleep=sleep,
421+
blocking=blocking,
414422
blocking_timeout=blocking_timeout,
415423
thread_local=thread_local,
416424
)

redis/asyncio/cluster.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -801,6 +801,7 @@ def lock(
801801
name: KeyT,
802802
timeout: Optional[float] = None,
803803
sleep: float = 0.1,
804+
blocking: bool = True,
804805
blocking_timeout: Optional[float] = None,
805806
lock_class: Optional[Type[Lock]] = None,
806807
thread_local: bool = True,
@@ -816,6 +817,12 @@ def lock(
816817
when the lock is in blocking mode and another client is currently
817818
holding the lock.
818819
820+
``blocking`` indicates whether calling ``acquire`` should block until
821+
the lock has been acquired or to fail immediately, causing ``acquire``
822+
to return False and the lock not being acquired. Defaults to True.
823+
Note this value can be overridden by passing a ``blocking``
824+
argument to ``acquire``.
825+
819826
``blocking_timeout`` indicates the maximum amount of time in seconds to
820827
spend trying to acquire the lock. A value of ``None`` indicates
821828
continue trying forever. ``blocking_timeout`` can be specified as a
@@ -858,6 +865,7 @@ def lock(
858865
name,
859866
timeout=timeout,
860867
sleep=sleep,
868+
blocking=blocking,
861869
blocking_timeout=blocking_timeout,
862870
thread_local=thread_local,
863871
)

tests/test_asyncio/test_lock.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,14 @@ async def test_float_timeout(self, r):
9797
assert 8 < (await r.pttl("foo")) <= 9500
9898
await lock.release()
9999

100+
async def test_blocking(self, r):
101+
blocking = False
102+
lock = self.get_lock(r, "foo", blocking=blocking)
103+
assert not lock.blocking
104+
105+
lock_2 = self.get_lock(r, "foo")
106+
assert lock_2.blocking
107+
100108
async def test_blocking_timeout(self, r, event_loop):
101109
lock1 = self.get_lock(r, "foo")
102110
assert await lock1.acquire(blocking=False)

0 commit comments

Comments
 (0)