Skip to content

Why doesn't asyncio.as_completed cancel unfinished tasks if it timeout? #99933

Description

@NewUserHa

Feature or enhancement

asyncio.as_completed should cancel unfinished tasks if it timeout, or even maybe have the ability to cancel no needed task

Pitch

def _on_timeout():
for f in todo:
f.remove_done_callback(_on_completion)
done.put_nowait(None) # Queue a dummy value for _wait_for_one().
todo.clear() # Can't do todo.remove(f) in the loop.
def _on_completion(f):
if not todo:
return # _on_timeout() was here first.
todo.remove(f)
done.put_nowait(f)
if not todo and timeout_handle is not None:
timeout_handle.cancel()
async def _wait_for_one():
f = await done.get()
if f is None:
# Dummy value from _on_timeout().
raise exceptions.TimeoutError
return f.result() # May raise f.exception().

Why doesn't asyncio.as_completed cancel unfinished tasks if it timeout?
Maybe say there may be many network requests pending, since that already raised exception then future requests should probably be canceled to save resources.
Probably because the concurrent.futures.as_complete didn't cancel unfinished tasks? But it's local resources than asyncio is usually network resources.

More, I know the asyncio.wait() can do cancel the pending job. But asyncio.as_complete() probably should also be able to cancel the next coros if got the wanted result. But currently, the returned is a coroutine that can't cancel.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Fields

    No fields configured for issues without a type.

    Projects

    Status
    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions