aioredis-py icon indicating copy to clipboard operation
aioredis-py copied to clipboard

Endless executing await connection.get() with cached_property

Open Veritaris opened this issue 3 years ago • 2 comments

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

Veritaris avatar Jul 27 '21 19:07 Veritaris

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.

seandstewart avatar Jul 27 '21 19:07 seandstewart

@Veritaris check if this helps: https://github.com/pydanny/cached-property#working-with-asyncawait-python-35

Andrew-Chen-Wang avatar Jul 28 '21 01:07 Andrew-Chen-Wang