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

feat: optimize exhaust_iterator #439

Merged
merged 2 commits into from
Nov 25, 2024
Merged
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
15 changes: 10 additions & 5 deletions a_sync/utils/iterators.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,11 @@ async def exhaust_iterator(
- :func:`exhaust_iterators`
- :func:`as_yielded`
"""
async for thing in iterator:
if queue:
if queue is None:
async for thing in iterator:
pass
else:
async for thing in iterator:
logger.debug("putting %s from %s to queue %s", thing, iterator, queue)
queue.put_nowait(thing)

Expand Down Expand Up @@ -71,19 +74,21 @@ async def exhaust_iterators(
- :func:`exhaust_iterator`
- :func:`as_yielded`
"""
if queue is None and join:
raise ValueError("You must provide a `queue` to use kwarg `join`")

for x in await asyncio.gather(
*[exhaust_iterator(iterator, queue=queue) for iterator in iterators],
return_exceptions=True,
):
if isinstance(x, Exception):
# raise it with its original traceback instead of from here
raise x.with_traceback(x.__traceback__)
if queue:

if queue is not None:
queue.put_nowait(_Done())
if join:
await queue.join()
elif join:
raise ValueError("You must provide a `queue` to use kwarg `join`")


T0 = TypeVar("T0")
Expand Down