-
Notifications
You must be signed in to change notification settings - Fork 112
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
Fix add_limit wrapper for async generators #1310
Closed
Closed
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
6e59ed3
Yield after checks in add_limit wrapper generator
sultaniman 99c0278
Rewrite wrap_async_iterator to use event loop
sultaniman 4708a7a
Skip mypy warnings
sultaniman b434c03
Remove unused mypy ignore comment
sultaniman bd94310
Adjust resource evaluation test
sultaniman c0f09d0
Remove breakpoint
sultaniman 5840fc5
Close async generator using event loop
sultaniman 27841cf
Mark tests for asyncio
sultaniman 8e7fcd1
Remove nonlocal definition
sultaniman File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -180,39 +180,20 @@ def wrap_async_iterator( | |
gen: AsyncIterator[TDataItems], | ||
) -> Generator[Awaitable[TDataItems], None, None]: | ||
"""Wraps an async generator into a list of awaitables""" | ||
exhausted = False | ||
busy = False | ||
|
||
# creates an awaitable that will return the next item from the async generator | ||
async def run() -> TDataItems: | ||
nonlocal exhausted | ||
loop = asyncio.get_event_loop() | ||
should_stop = False | ||
try: | ||
try: | ||
# if marked exhausted by the main thread and we are wrapping a generator | ||
# we can close it here | ||
if exhausted: | ||
raise StopAsyncIteration() | ||
item = await gen.__anext__() | ||
return item | ||
# on stop iteration mark as exhausted | ||
# also called when futures are cancelled | ||
while True: | ||
if should_stop: | ||
break | ||
yield loop.run_until_complete(gen.__anext__()) # type: ignore[arg-type] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I used eventloop because with the old implementation it has been complaining about not awaited generator when resource combined with tests/extract/test_sources.py::test_add_limit_async
/[...]/dlt/dlt/extract/pipe_iterator.py:275: RuntimeWarning: coroutine 'wrap_async_iterator.<locals>.run' was never awaited
pipe_item = next(gen)
Enable tracemalloc to get traceback where the object was allocated.
See https://docs.pytest.org/en/stable/how-to/capture-warnings.html#resource-warnings for more info. |
||
except StopAsyncIteration: | ||
exhausted = True | ||
raise | ||
finally: | ||
nonlocal busy | ||
busy = False | ||
|
||
# this generator yields None while the async generator is not exhausted | ||
try: | ||
while not exhausted: | ||
while busy: | ||
yield None | ||
busy = True | ||
yield run() | ||
# this gets called from the main thread when the wrapping generater is closed | ||
should_stop = True | ||
except GeneratorExit: | ||
# mark as exhausted | ||
exhausted = True | ||
should_stop = True | ||
if hasattr(gen, "aclose"): | ||
loop.run_until_complete(gen.aclose()) | ||
|
||
|
||
def wrap_parallel_iterator(f: TAnyFunOrGenerator) -> TAnyFunOrGenerator: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we need to yield only after
if
section