From d34fb5e1fc7b96544f16ceb5a8b2facc4e3759df Mon Sep 17 00:00:00 2001 From: Hauke D Date: Tue, 4 Nov 2025 11:44:48 +0100 Subject: [PATCH] Add max_tries to transaction() --- valkey/asyncio/client.py | 5 +++++ valkey/client.py | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/valkey/asyncio/client.py b/valkey/asyncio/client.py index 10c47818..fa0cf27e 100644 --- a/valkey/asyncio/client.py +++ b/valkey/asyncio/client.py @@ -437,6 +437,7 @@ async def transaction( shard_hint: Optional[str] = None, value_from_callable: bool = False, watch_delay: Optional[float] = None, + max_tries: Optional[int] = None, ): """ Convenience method for executing the callable `func` as a transaction @@ -445,7 +446,11 @@ async def transaction( """ pipe: Pipeline async with self.pipeline(True, shard_hint) as pipe: + tries = 0 while True: + tries += 1 + if max_tries and max_tries>0 and tries>max_tries: + raise ValkeyError(f"Bailing out of transaction after {tries-1} tries") try: if watches: await pipe.watch(*watches) diff --git a/valkey/client.py b/valkey/client.py index 88703129..d534933e 100755 --- a/valkey/client.py +++ b/valkey/client.py @@ -408,8 +408,13 @@ def transaction( shard_hint = kwargs.pop("shard_hint", None) value_from_callable = kwargs.pop("value_from_callable", False) watch_delay = kwargs.pop("watch_delay", None) + max_tries = kwargs.pop("max_tries", None) with self.pipeline(True, shard_hint) as pipe: + tries = 0 while True: + tries += 1 + if max_tries and max_tries>0 and tries>max_tries: + raise ValkeyError(f"Bailing out of transaction after {tries-1} tries") try: if watches: pipe.watch(*watches)