I created a small class to perform basic operations with redis, using aioredis.
class RedisService:
def __init__(self, r_url) -> str:
self.redis = r_url
async def create_connection(self):
return await aioredis.create_redis(self.redis)
async def _get(self, key) -> str:
try:
return await self.create_connection().get(key, encoding='utf-8')
finally:
await self._close()
async def _set(self, key, value) -> None:
await self.create_connection().set(key, value)
await self._close()
async def _close(self) -> None:
self.create_connection().close()
await self._redis.wait_closed()
And a test handler to call the write/read operation for redis
@router.post('/perform')
async def index():
key = 'test'
value = 'test'
value = await RedisService(r_url)._set(key, value)
return {'result': value}
But get error
await self.create_connection.set(key, value)
AttributeError: ''coroutine'' object has no attribute 'set'
I guess the problem could be that the asynchronous code has to be run through an event loop
asyncio.run(some coroutine)
But I can't understand how I can build this logic into my code