achimnol / aiotools

Idiomatic asyncio utilties
https://aiotools.readthedocs.io
MIT License
153 stars 11 forks source link

Implement an async exit stack for composing async context managers #11

Closed software-opal closed 6 years ago

software-opal commented 6 years ago

In the standard library there is contextlib.ExitStack which allows multiple context managers to be applied one inside the other such that a failure of an __enter__ method calls the __exit__ methods.

If this could be implemented in such a way that it would allow for both sync and async context managers to be called; and called inside to out with appropriate error handling.

achimnol commented 6 years ago

aiotools already has a similar functionality: AsyncContextGroup. While contextlib.ExitStack is for sequentially stacking multiple context managers, aiotools.AsyncContextGroup executes enter/exit handlers of multiple async context managers concurrently using asyncio.gather.

It may be useful to have a sequential equivalent of ExitStack but I don't see the enough motivation because it does not exploit asynchrony. Please bring me a motivating example for that. :)