yahoo / redislite

Redis in a python module.
Other
583 stars 74 forks source link

Feature request: client as context manager #125

Open clbarnes opened 5 years ago

clbarnes commented 5 years ago

There are a few cases where redislite instances are useful to spin up during the execution of a script, and then make sure they're cleared up afterwards (tests etc.). One can manually shutdown the server and/or del the instance, which can be done in a try/except to be error-tolerant, but this is a perfect use case for a context manager which would call _cleanup on __exit__.

It could be as simple as adding

class RedisMixin(object):
    ...

    def __enter__(self):
        return self

    def __exit__(exc_type, exc_val, exc_tb):
        self._cleanup()
        # self.shutdown()  # possibly

Although we'd probably want to make sure we didn't cleanup/ shutdown a server we didn't start.

Somewhat related to #97

dwighthubbard commented 5 years ago

Yes, that sounds like a good idea to me.