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.
Feature or enhancement
asyncio.as_completedshould cancel unfinished tasks if it timeout, or even maybe have the ability to cancel no needed taskPitch
cpython/Lib/asyncio/tasks.py
Lines 588 to 609 in 0563be2
Why doesn't
asyncio.as_completedcancel 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_completedidn'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. Butasyncio.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.