Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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
mivade wants to merge5 commits intopython:mainfrommivade:async-as-completed
Closed
Changes from1 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
PrevPrevious commit
NextNext commit
Use timeout handling from previous implementation
  • Loading branch information
@mivade
mivade committedOct 31, 2018
commit67a46d60caf2f13e94e2ec6c0af720e55ac38f10
49 changes: 34 additions & 15 deletionsLib/asyncio/tasks.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -14,7 +14,6 @@
import functools
import inspect
import itertools
import time
import types
import warnings
import weakref
Expand DownExpand Up@@ -545,36 +544,56 @@ def __init__(self, fs, *, loop=None, timeout=None):

self._loop = loop
self._timeout = timeout
self._timeout_handle = None

for future in self._pending:
future.add_done_callback(self._done_callback)
future.add_done_callback(self._on_completion)

def_done_callback(self, future):
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 = await self._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):
try:
t0 = time.time()
self._start_timeout()

try:
while self._pending:
timeout = (self._timeout - (time.time() - t0)
if self._timeout is not None else None)

future = await wait_for(self._completed.get(),
timeout,
loop=self._loop)
future = await self._completed.get()
yield future.result()
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._done_callback)
future.remove_done_callback(self._on_completion)

def __iter__(self):
while self._pending:
future = self._completed.get()
yield future
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
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp