Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix blocking on async pipeline with many commands (>100) #3448

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Fix blocking on async pipeline with many commands (200k)
rad-pat committed Dec 4, 2024
commit c209bf52f499b45611568ee8b15d0ab96e2d6ffe
1 change: 1 addition & 0 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
* Fix #3416, prevent blocking in async Pipeline with many commands
* Move doctests (doc code examples) to main branch
* Update `ResponseT` type hint
* Allow to control the minimum SSL version
14 changes: 11 additions & 3 deletions redis/asyncio/client.py
Original file line number Diff line number Diff line change
@@ -1362,8 +1362,10 @@ async def _execute_transaction( # noqa: C901
pre: CommandT = (("MULTI",), {})
post: CommandT = (("EXEC",), {})
cmds = (pre, *commands, post)
all_cmds = connection.pack_commands(
args for args, options in cmds if EMPTY_RESPONSE not in options
# Run pack_commands in a thread to prevent blocking
all_cmds = await asyncio.to_thread(
connection.pack_commands,
(args for args, options in cmds if EMPTY_RESPONSE not in options)
)
await connection.send_packed_command(all_cmds)
errors = []
@@ -1387,6 +1389,9 @@ async def _execute_transaction( # noqa: C901
except ResponseError as err:
self.annotate_exception(err, i + 1, command[0])
errors.append((i, err))
# Release back to event loop to prevent blocking
if i % 100 == 0:
await asyncio.sleep(0)

# parse the EXEC.
try:
@@ -1419,7 +1424,8 @@ async def _execute_transaction( # noqa: C901

# We have to run response callbacks manually
data = []
for r, cmd in zip(response, commands):
# Enumerate and then release back to event loop to prevent blocking
for i, (r, cmd) in enumerate(zip(response, commands)):
if not isinstance(r, Exception):
args, options = cmd
command_name = args[0]
@@ -1432,6 +1438,8 @@ async def _execute_transaction( # noqa: C901
if inspect.isawaitable(r):
r = await r
data.append(r)
if i % 100 == 0:
await asyncio.sleep(0)
return data

async def _execute_pipeline(