I have a class that has an attribute that holds a Redis connection as seen below:
import redis
class RedisService:
db = redis.Redis(host=RedisConfig.REDIS_HOST, port=RedisConfig.REDIS_PORT)
@staticmethod
def exists(key):
return RedisService.db.exists(key)
This works well. But now I want to replace this with async alternatives:
import aioredis
class RedisService:
db = await aioredis.create_connection((RedisConfig.REDIS_HOST, RedisConfig.REDIS_PORT))
@staticmethod
async def exists(key):
value = await RedisService.db.execute('GET', key)
return value
But await
is not allowed to assign a value to a class attribute. If I remove await, when I call RedisService.exists()
I get:
File "./src/service/redis.py", line 12, in exists
value = await RedisService.db.execute('GET', key)
AttributeError: 'coroutine' object has no attribute 'execute'
So how can I assign a value to a class attribute by awaiting a coroutine?
Note that, I directly call await RedisService.exists()
without creating an object instance. It should be a static method and should've been called without creating an instance for some BL reason.