I was asked to post the discussion here from here.
I have a class written to interact with my Redis DB. In it, I thought about using a method for session handling (as I would do with other DBs) (exception handling is handled elsewhere):
@contextlib.contextmanager
def _make_session(self) -> Generator[redis.client.Pipeline]:
session = self.client.pipeline()
try:
yield session
finally:
session.close()
Imagine that I perform a with istruction to open the Pipeline, running one or more commands and executing them. e.g.:
with self._make_session() as session:
return method(self, session, *args, **kwargs)
I noticed that the code above only works the first time in my backend, the latters raise a NoneType error. So I looked into the Pipeline code and found out that close calls the reset method that has this part at the end:
if self.connection:
self.connection_pool.release(self.connection)
self.connection = None
Was this done for some reason in particular? It seems like that since the connection closes, from the second time I create a session it simply points to None. I had to quickly fix it like this:
@contextlib.contextmanager
def _make_session(self) -> Generator[redis.client.Pipeline]:
yield self.client.pipeline()
Do you have any ideas of why?
Comment From: sundb
😅you're in the wrong place again.
Comment From: Frank995
Damn I didn't notice there were two redis repos, sorry my bad