Skip to content

Commit

Permalink
Async: added 'blocking' argument to call lock method (#2454)
Browse files Browse the repository at this point in the history
Co-authored-by: Chayim <chayim@users.noreply.github.com>
  • Loading branch information
Sibuken and chayim authored Dec 1, 2022
1 parent 2c12155 commit c48dc83
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
8 changes: 8 additions & 0 deletions redis/asyncio/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,7 @@ def lock(
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,
Expand All @@ -369,6 +370,12 @@ def lock(
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
Expand Down Expand Up @@ -411,6 +418,7 @@ def lock(
name,
timeout=timeout,
sleep=sleep,
blocking=blocking,
blocking_timeout=blocking_timeout,
thread_local=thread_local,
)
Expand Down
8 changes: 8 additions & 0 deletions redis/asyncio/cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -801,6 +801,7 @@ def lock(
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,
Expand All @@ -816,6 +817,12 @@ def lock(
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
Expand Down Expand Up @@ -858,6 +865,7 @@ def lock(
name,
timeout=timeout,
sleep=sleep,
blocking=blocking,
blocking_timeout=blocking_timeout,
thread_local=thread_local,
)
Expand Down
8 changes: 8 additions & 0 deletions tests/test_asyncio/test_lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,14 @@ async def test_float_timeout(self, r):
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)
Expand Down

0 comments on commit c48dc83

Please sign in to comment.