Skip to content
Open
Changes from 1 commit
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
Prev Previous commit
Next Next commit
avoid container
  • Loading branch information
ebonnal committed Mar 20, 2025
commit 268927d965e1a369e7f49d0d56253dd9ae89717b
9 changes: 5 additions & 4 deletions Lib/concurrent/futures/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -621,8 +621,6 @@ def map(self, fn, *iterables, timeout=None, chunksize=1, buffersize=None):
# collected independently of the result_iterator closure.
executor_weakref = weakref.ref(self)

# used by the result_iterator to avoid keeping a reference to the result
result = collections.deque(maxlen=1)
# Yield must be hidden in closure so that the futures are submitted
# before the first iterator value is required.
def result_iterator():
Expand All @@ -635,16 +633,19 @@ def result_iterator():
if current_timeout is not None:
current_timeout = end_time - time.monotonic()

result.append(_result_or_cancel(fs.pop(), current_timeout))
# wait for the next result
_result_or_cancel(fs[-1], current_timeout)

# buffer next task
if (
buffersize
and (executor := executor_weakref())
and (args := next(zipped_iterables, None))
):
fs.appendleft(executor.submit(fn, *args))

yield result.pop()
# yield the awaited result
yield fs.pop().result()

@ebonnal ebonnal Mar 22, 2025 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to be discussed: this could be replaced by a lighter yield fs.pop()._result because the prior call to _result_or_cancel guarantees that at this point the result is available.

finally:
for future in fs:
future.cancel()
Expand Down