Open
Description
I've come to the library from aredis
and running my same code, a web application built on aiohttp.
I'm wondering why this library warns if a redis connection has not been closed in the __del__
function, rather than just closing it?
Unclosed RedisCluster client
client: <redis.asyncio.cluster.RedisCluster object at 0x7f86d1c27280>
Although it now is obvious, It took me a while to track down what exactly was triggering the warning; I previously didn't know that connections were expected to await aclose
from aiohttp import web
from redis.asyncio.cluster import RedisCluster
async def endpoint(request=False, source=''):
red = RedisCluster(
REDIS_CX[0], REDIS_CX[1], password=REDIS_PWD,
decode_responses=decode_responses,
)
...
await red.aclose() # <----------------FIX
return web.json_response(ret)
if __name__ == '__main__':
app = web.Application()
app.add_routes([
web.get('/endpoint', endpoint),
])
web.run_app(app, host=ip_host, port=port, ssl_context=ssl_context)
So my questions are:
- Am I doing it wrong in terms of creating a new
RedisCluster
with each request; should I be opening up a pool first? The warning implies that these operations are more expensive than I was thinking - Could the
await red.aclose()
there potentially be expensive and slow down the response slightly? - If none of the above, should the
__del__
function (presumably triggered by garbage collection) just go ahead and close the connection for me without emitting a warning?
I'd be happy to update docs etc. if there are changes needed.