Author andreash
Recipients andreash, ncoghlan, yselivanov
Date 2021-09-13.14:40:01
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1631544002.04.0.855227455654.issue45184@roundup.psfhosted.org>
In-reply-to
Content
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. https://stackoverflow.com/a/37607405. 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
            else:
                self._exit_callbacks.append(cb)
        if not found:
            raise KeyError("context manager not found")
        if found[0]:
            return found[1](None,None,None)
        else:
            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.


Use-Cases:

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.
History
Date User Action Args
2021-09-13 14:40:02andreashsetrecipients: + andreash, ncoghlan, yselivanov
2021-09-13 14:40:02andreashsetmessageid: <1631544002.04.0.855227455654.issue45184@roundup.psfhosted.org>
2021-09-13 14:40:01andreashlinkissue45184 messages
2021-09-13 14:40:01andreashcreate