Asyncio TimeoutGroup

I’d like to propose a feature in some aspects similar to the TaskGroup, hence the working name.

Let’s have a coroutine t10 with a timeout of 10 seconds and t30 with a timeout of 30 seconds. Let’s assume those timeouts are reasonable values and both coroutines need much less time under usual circumstances.

When running both as two concurrent tasks with their respective timeouts, it can happen that t30 takes say 20 seconds to complete, but t10 needs more retrying. Everything is fine for t30, but t10 gets cancelled after 10 seconds even if there was room to let it run 10 seconds longer “for free” and thus increase its chances to succeed.

The idea is simple: Allow a longer timeout if another task in the group is running and didn’t reach its timeout limit yet. Example usage:

async with asyncio.TimeoutGroup() as tog:
    tog.create_task(t10(), timeout=10)    # timeout - new mandatory parameter
    tog.create_task(t30(), timeout=30)

Some of you will find the gain too small and will propose e.g. to run everyting with the longest timeout of all timeouts. I was thinking about it, but decided to post.

1 Like

Do you have code that would benifit from this feature?
What is the use case that you have where this would be useful?

My only, but main use case is async shutdown of my async apps. Expected are both a quick reaction (short restart time) and a good cleanup (external HW is sometimes involved). In other words, I don’t want to make the shutdown longer than necessary, but if it takes some time, I want to use that time as much as possible.

Sounds somewhat reasonable to me, but wouldn’t it be possible for users to create their own TimeoutGroup context manager, which gets the max timeout, and runs the tasks like you described?!