Skip to content

Commit 4d7de6d

Browse files
authored
Add support for BLMPOP (#1849)
* Add support for BLMPOP * add type hints * fix test * fix comment * fix pr comment * delete count check * change numkeys * linters * mark test as onlynoncluster
1 parent aaddeb6 commit 4d7de6d

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

redis/commands/core.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1917,6 +1917,27 @@ def brpoplpush(self, src, dst, timeout=0):
19171917
timeout = 0
19181918
return self.execute_command("BRPOPLPUSH", src, dst, timeout)
19191919

1920+
def blmpop(
1921+
self,
1922+
timeout: float,
1923+
numkeys: int,
1924+
*args: List[str],
1925+
direction: str,
1926+
count: Optional[int] = 1,
1927+
) -> Optional[list]:
1928+
"""
1929+
Pop ``count`` values (default 1) from first non-empty in the list
1930+
of provided key names.
1931+
1932+
When all lists are empty this command blocks the connection until another
1933+
client pushes to it or until the timeout, timeout of 0 blocks indefinitely
1934+
1935+
For more information check https://redis.io/commands/blmpop
1936+
"""
1937+
args = [timeout, numkeys, *args, direction, "COUNT", count]
1938+
1939+
return self.execute_command("BLMPOP", *args)
1940+
19201941
def lmpop(
19211942
self,
19221943
num_keys: int,

tests/test_commands.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1480,6 +1480,18 @@ def test_brpoplpush_empty_string(self, r):
14801480
r.rpush("a", "")
14811481
assert r.brpoplpush("a", "b") == b""
14821482

1483+
@pytest.mark.onlynoncluster
1484+
# @skip_if_server_version_lt("7.0.0") turn on after redis 7 release
1485+
def test_blmpop(self, unstable_r):
1486+
unstable_r.rpush("a", "1", "2", "3", "4", "5")
1487+
res = [b"a", [b"1", b"2"]]
1488+
assert unstable_r.blmpop(1, "2", "b", "a", direction="LEFT", count=2) == res
1489+
with pytest.raises(TypeError):
1490+
unstable_r.blmpop(1, "2", "b", "a", count=2)
1491+
unstable_r.rpush("b", "6", "7", "8", "9")
1492+
assert unstable_r.blmpop(0, "2", "b", "a", direction="LEFT") == [b"b", [b"6"]]
1493+
assert unstable_r.blmpop(1, "2", "foo", "bar", direction="RIGHT") is None
1494+
14831495
@pytest.mark.onlynoncluster
14841496
# @skip_if_server_version_lt("7.0.0") turn on after redis 7 release
14851497
def test_lmpop(self, unstable_r):

0 commit comments

Comments
 (0)