migrate existing context manager wrapper v0.3 to v1.0 #443
Comments
k- so I'm already see problems with above (the pool is not releasing it's connections). This is what I had to do (and it seems to work, but again I'm not sure if I'm doing bad things to this library) import aioredis
import config
class RedisWrapper:
__slots__ = ("_instance", )
_pool = None
def __init__(self):
self._instance = None
async def __aenter__(self):
self._instance = await self.get_pool()
return self._instance # still don't know what aioredlock was achieving with the await
async def __aexit__(self, exc_type, exc_val, exc_tb):
await self._instance.quit() # releases "the connection" which I'm assuming means the connection I had(and not some other connection), must be awaited or doesn't work reliably - again I'm guessing
self._instance = None
async def get_pool(self):
if not self._pool:
self._pool = await aioredis.create_redis_pool(
(config.host, config.port),
minsize=5,
maxsize=10
)
return self._pool |
First of all seems you need Then you really do not need acquire connection from pool for non-blocking commands. |
Just in case anyone here is only following the aioredis thread: From a conversation with @akhilman and some more investigation into import aioredis
from aioredis import ConnectionsPool, Redis
import config
class RedisWrapper:
__slots__ = ("_instance", )
_pool = None
def __init__(self):
self._instance = None
async def __aenter__(self) -> Redis:
pool = await self.get_pool()
self._instance = await pool.acquire()
return Redis(self._instance)
async def __aexit__(self, exc_type, exc_val, exc_tb):
pool = await self.get_pool()
pool.release(self._instance)
self._instance = None
async def get_pool(self) -> ConnectionsPool:
if not self._pool:
self._pool = await aioredis.create_pool(
(config.host, config.port),
minsize=5,
maxsize=10
)
return self._pool again, from my initial tests it seems to work better than the previous implementation (and also avoids using |
I've been actively using aioredis v0.3 up until I attempted to move to python 3.7 which triggered the need to do an update to v1. I've put aioredis inside a wrapper class that retains this interface:
I have a feeling that I may have been using it wrong all along as my set up totally crumbled with the move.
Here's my current set up under 0.3
I've been reading through the migration description and it looks like interaction has changed such that now I'm trying to make the move to the
aioredis.create_redis_pool
(as that seems like the closest fit). After many failed attempts, however, I don't know how to (or if I even have to) release the connection during the clean up in__aexit__
. I've been looking around to see what others do and I've noticed the following usage inaioredlock
(line 118-128):and then interacted with here(line 142-147):
which leads me to believe I could just do this in v1:
This set up seems to work, but I'm just copying code, and would like a confirmation that I'm not abusing the library.
The text was updated successfully, but these errors were encountered: