Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork32k
gh-77714: Implement as_completed as an asynchronous generator#10251
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Closed
Uh oh!
There was an error while loading.Please reload this page.
Closed
Changes fromall commits
Commits
Show all changes
5 commits Select commitHold shift + click to select a range
1e5d05c
Implement as_completed as an asynchronous iterator
mivade67a46d6
Use timeout handling from previous implementation
mivade52b10da
Fix import
mivadeab8e762
Yield a future with async for
mivade03b0561
Merge branch 'main' into async-as-completed
mivadeFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 | ||||
---|---|---|---|---|---|---|
@@ -570,62 +570,79 @@ async def _cancel_and_wait(fut): | ||||||
fut.remove_done_callback(cb) | ||||||
class as_completed(object): | ||||||
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. Suggested change
Inheriting from object isn't necessary | ||||||
"""Asynchronous iterator over awaitables which yields each future as it | ||||||
is completed consistent with PEP 3148. Usage:: | ||||||
async for future in as_completed(fs): | ||||||
print(future.result()) | ||||||
For backwards compatibility, this can also be used as a regular iterator:: | ||||||
for f in as_completed(fs): | ||||||
result = await f # The 'await' may raise. | ||||||
# Use result. | ||||||
""" | ||||||
def __init__(self, fs, *, timeout=None): | ||||||
if futures.isfuture(fs) or coroutines.iscoroutine(fs): | ||||||
raise TypeError(f"expect an iterable of futures, not {type(fs).__name__}") | ||||||
from .queues import Queue # Import here to avoid circular import problem. | ||||||
self._loop = events.get_event_loop() | ||||||
self._pending = set(ensure_future(f, loop=self._loop) for f in fs) | ||||||
self._completed = Queue() | ||||||
self._timeout = timeout | ||||||
self._timeout_handle = None | ||||||
for future in self._pending: | ||||||
future.add_done_callback(self._on_completion) | ||||||
def _on_completion(self, future): | ||||||
self._pending.remove(future) | ||||||
self._completed.put_nowait(future) | ||||||
if not self._pending and self._timeout_handle is not None: | ||||||
self._timeout_handle.cancel() | ||||||
async def _wait_for_one(self): | ||||||
f = awaitself._completed.get() | ||||||
if f is None: | ||||||
# Dummy value from _on_timeout(). | ||||||
raise exceptions.TimeoutError | ||||||
return f.result() # May raise f.exception(). | ||||||
def _on_timeout(self): | ||||||
for f in self._pending: | ||||||
f.remove_done_callback(self._on_completion) | ||||||
def _start_timeout(self): | ||||||
if self._pending and self._timeout is not None: | ||||||
self._timeout_handle = self._loop.call_later(self._timeout, | ||||||
self._on_timeout) | ||||||
async def __aiter__(self): | ||||||
self._start_timeout() | ||||||
try: | ||||||
while self._pending: | ||||||
future = await self._completed.get() | ||||||
yield future | ||||||
finally: | ||||||
# If an exception happened, we want to ensure that the done | ||||||
# callback doesn't run anyway | ||||||
for future in self._pending: | ||||||
future.remove_done_callback(self._on_completion) | ||||||
def __iter__(self): | ||||||
for f in self._pending: | ||||||
f.add_done_callback(self._on_completion) | ||||||
self._start_timeout() | ||||||
for _ in range(len(self._pending)): | ||||||
yield self._wait_for_one() | ||||||
@types.coroutine | ||||||
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.