Skip to content

Commit 3908ac8

Browse files
committed
improved generator timeout handling
1 parent 157c39b commit 3908ac8

1 file changed

Lines changed: 8 additions & 2 deletions

File tree

python_utils/generators.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import asyncio
12
import time
23

34
import python_utils
@@ -26,8 +27,13 @@ async def abatcher(
2627

2728
while True:
2829
try:
29-
item = await generator.__anext__()
30-
except StopAsyncIteration:
30+
if interval_s:
31+
item = await asyncio.wait_for(
32+
generator.__anext__(), interval_s
33+
)
34+
else:
35+
item = await generator.__anext__()
36+
except (StopAsyncIteration, asyncio.TimeoutError):
3137
if batch:
3238
yield batch
3339
break

0 commit comments

Comments
 (0)