[HiCache] fix: graceful shutdown of pending async tasks in bench_mix.py (#20276)

This commit is contained in:
shuwenn
2026-03-29 00:46:32 -07:00
committed by GitHub
parent d2fa8d67ba
commit c34593f951
+8 -1
View File
@@ -426,11 +426,13 @@ class WorkloadGenerator:
def request_sender(self):
async def request_loop():
tasks = []
while True:
if self.sent_requests - self.completed_requests < self.max_parallel:
new_request = self.user_generator.pop()
if new_request:
asyncio.create_task(self.handle_request(new_request))
task = asyncio.create_task(self.handle_request(new_request))
tasks.append(task)
self.sent_requests += 1
else:
await asyncio.sleep(0.05)
@@ -440,6 +442,11 @@ class WorkloadGenerator:
self.done = True
break
# Cancel all pending tasks and wait for them to finish
for task in tasks:
task.cancel()
await asyncio.gather(*tasks, return_exceptions=True)
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.run_until_complete(request_loop())