aioredis-py
aioredis-py copied to clipboard
Endless executing await connection.get() with cached_property
Hi
During solving a problem with having only one connection (or pool, this issue is also actual for result of aioredis.create_redis_pool()
) I'm stack with such error
When I get connection with the following code
class A:
@cached_property
async def connection(self):
address = f"redis://{self.redis_host}:{self.redis_port}"
connection = await aioredis.create_redis(
address=address,
password=self.redis_password
)
return connection
... some other code ....
async def get_data(self):
connection = await self.connection
some_data = await connection.get("some_key")
return some_data
at first time there is two requests to redis - AUTH and then GET. If I try to call get_data another one time in this runtime there will be only GET request and code will stack after some_data = await connection.get("some_key")
, e.g. some_data
will never be awaited
Creating new connection every time works fine, but this is not good and why not to reuse same connection (cause we also can have pool of them)
What this problem can be connected with?
I've also tried to disable Redis auth, this didn't help
Using aioredis==1.3.1
and cached-property==1.5.2
Hi @Veritaris -
Can you try without the cached-property?
IIRC, cached-property caches the result of the first function call.
In this case, your function returns a coroutine, which you then await, allowing you to retrieve the return value of the coroutine, which is a connection instance.
cached-property is caching the original coroutine, which can no longer be awaited, as it's already been executed and you've already retrieved the result.
@Veritaris check if this helps: https://github.com/pydanny/cached-property#working-with-asyncawait-python-35