-
Notifications
You must be signed in to change notification settings - Fork 46
Backport fixes to as_completed and map iterators (bpo-27144) #66
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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 |
|---|---|---|
|
|
@@ -172,6 +172,29 @@ def _create_and_install_waiters(fs, return_when): | |
|
|
||
| return waiter | ||
|
|
||
|
|
||
| def _yield_finished_futures(fs, waiter, ref_collect): | ||
| """ | ||
| Iterate on the list *fs*, yielding finished futures one by one in | ||
| reverse order. | ||
| Before yielding a future, *waiter* is removed from its waiters | ||
| and the future is removed from each set in the collection of sets | ||
| *ref_collect*. | ||
|
|
||
| The aim of this function is to avoid keeping stale references after | ||
| the future is yielded and before the iterator resumes. | ||
| """ | ||
| while fs: | ||
| f = fs[-1] | ||
| for futures_set in ref_collect: | ||
| futures_set.remove(f) | ||
| with f._condition: | ||
| f._waiters.remove(waiter) | ||
| del f | ||
| # Careful not to keep a reference to the popped value | ||
| yield fs.pop() | ||
|
|
||
|
|
||
| def as_completed(fs, timeout=None): | ||
| """An iterator over the given futures that yields each as it completes. | ||
|
|
||
|
|
@@ -194,16 +217,19 @@ def as_completed(fs, timeout=None): | |
| end_time = timeout + time.time() | ||
|
|
||
| fs = set(fs) | ||
| total_futures = len(fs) | ||
| with _AcquireFutures(fs): | ||
| finished = set( | ||
| f for f in fs | ||
| if f._state in [CANCELLED_AND_NOTIFIED, FINISHED]) | ||
| pending = fs - finished | ||
| waiter = _create_and_install_waiters(fs, _AS_COMPLETED) | ||
|
|
||
| finished = list(finished) | ||
| try: | ||
| for future in finished: | ||
| yield future | ||
| for f in _yield_finished_futures(finished, waiter, | ||
| ref_collect=(fs,)): | ||
| f = [f] | ||
| yield f.pop() | ||
|
|
||
| while pending: | ||
| if timeout is None: | ||
|
|
@@ -213,7 +239,7 @@ def as_completed(fs, timeout=None): | |
| if wait_timeout < 0: | ||
| raise TimeoutError( | ||
| '%d (of %d) futures unfinished' % ( | ||
| len(pending), len(fs))) | ||
| len(pending), total_futures)) | ||
|
|
||
| waiter.event.wait(wait_timeout) | ||
|
|
||
|
|
@@ -222,11 +248,15 @@ def as_completed(fs, timeout=None): | |
| waiter.finished_futures = [] | ||
| waiter.event.clear() | ||
|
|
||
| for future in finished: | ||
| yield future | ||
| pending.remove(future) | ||
| # reverse to keep finishing order | ||
| finished.reverse() | ||
| for f in _yield_finished_futures(finished, waiter, | ||
| ref_collect=(fs, pending)): | ||
| f = [f] | ||
|
Owner
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. Ditto |
||
| yield f.pop() | ||
|
|
||
| finally: | ||
| # Remove waiter from unfinished futures | ||
| for f in fs: | ||
| with f._condition: | ||
| f._waiters.remove(waiter) | ||
|
|
@@ -600,11 +630,14 @@ def map(self, fn, *iterables, **kwargs): | |
| # before the first iterator value is required. | ||
| def result_iterator(): | ||
| try: | ||
| for future in fs: | ||
| # reverse to keep finishing order | ||
| fs.reverse() | ||
| while fs: | ||
| # Careful not to keep a reference to the popped future | ||
| if timeout is None: | ||
| yield future.result() | ||
| yield fs.pop().result() | ||
| else: | ||
| yield future.result(end_time - time.time()) | ||
| yield fs.pop().result(end_time - time.time()) | ||
| finally: | ||
| for future in fs: | ||
| future.cancel() | ||
|
|
||
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.
Why this? Why not just
yield f?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.
Ahhh...because otherwise the future would still be referenced by the generator. Gotcha.
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.
Indeed. Maybe Python 4 will consider fixing iteration target vars leaking to the scope :-).