Author andreash
Recipients andreash, ncoghlan, yselivanov
Date 2021-09-13.14:40:01
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>
Currently it is not possible to remove context managers from an ExitStack (or AsyncExitStack). 

Workarounds are difficult and generally do accesses implementation details of (Async)ExitStack.
See e.g. It could be done as follows:

class AsyncExitStackWithPop(contextlib.AsyncExitStack):
    """Same as AsyncExitStack but with pop, i.e. removal functionality"""
    async def pop(self, cm):
        callbacks = self._exit_callbacks
        self._exit_callbacks = collections.deque()
        found = None
        while callbacks:
            cb = callbacks.popleft()
            if cb[1].__self__ == cm:
                found = cb
        if not found:
            raise KeyError("context manager not found")
        if found[0]:
            return found[1](None,None,None)
            return await found[1](None, None, None)

The alternative is re-implementation of ExitStack with pop functionality, but that is also very difficult to get right (especially with exceptions). Which is probably the reason why there is ExitStack in the library at all.

So I propose to augment (Async)ExitStack with a `pop` method like above or similar to the above.


An example is a component that manages several connections to network services. 
During run-time the network services might need to change (i.e. some be disconnected and some be connected according to business logic), or handle re-connection events (ie. graceful response to network errors).
It is not too hard to imagine more use cases.
Essentially every case where dynamic resource management is needed and where single resources are managable with python context managers.
Date User Action Args
2021-09-13 14:40:02andreashsetrecipients: + andreash, ncoghlan, yselivanov
2021-09-13 14:40:02andreashsetmessageid: <>
2021-09-13 14:40:01andreashlinkissue45184 messages
2021-09-13 14:40:01andreashcreate