Skip to content

Commit

Permalink
check if async iterator has aclose before aclosing it
Browse files Browse the repository at this point in the history
  • Loading branch information
kramstrom committed Dec 3, 2024
1 parent ea5a860 commit c6c978f
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions modal/_utils/async_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import itertools
import time
import typing
from collections.abc import AsyncGenerator, Awaitable, Iterable, Iterator
from collections.abc import AsyncGenerator, AsyncIterator, Awaitable, Iterable, Iterator
from contextlib import asynccontextmanager
from dataclasses import dataclass
from typing import (
Expand Down Expand Up @@ -484,14 +484,17 @@ async def __aexit__(self, exc, exc_type, tb):
await self.agen.aclose()


async def sync_or_async_iter(iter: Union[Iterable[T], AsyncGenerator[T, None]]) -> AsyncGenerator[T, None]:
async def sync_or_async_iter(iter: Union[Iterable[T], AsyncIterator[T, None]]) -> AsyncGenerator[T, None]:
if hasattr(iter, "__aiter__"):
agen = typing.cast(AsyncGenerator[T, None], iter)
try:
async for item in agen:
yield item
finally:
await agen.aclose()
if hasattr(agen, "aclose"):
# All AsyncGenerators have an aclose method
# but some AsyncIterators don't necessarily
await agen.aclose()
else:
assert hasattr(iter, "__iter__"), "sync_or_async_iter requires an Iterable or AsyncGenerator"
# This intentionally could block the event loop for the duration of calling __iter__ and __next__,
Expand Down

0 comments on commit c6c978f

Please sign in to comment.