This commit is contained in:
2025-04-11 15:04:22 +08:00
parent 1d9ab14b01
commit dcceb275ad
5 changed files with 105 additions and 22 deletions

View File

@@ -0,0 +1,18 @@
import asyncio
from collections.abc import AsyncGenerator
from typing import TypeVar
T = TypeVar('T')
async def agzip(*async_generators: AsyncGenerator[T, None]) -> AsyncGenerator[tuple[T, ...], None]:
"""
`zip()`-like function for `AsyncGenerator`s.
"""
iterators = [ag.__aiter__() for ag in async_generators]
while True:
try:
results = await asyncio.gather(*[iterator.__anext__() for iterator in iterators])
yield tuple(results)
except StopAsyncIteration:
break