问题
I want to use aioredis connection pool ,not create new connection in every AsyncWebsocketConsumer.connect .Where can I put the code.
Now I create new connection in connect event every time, code like this
async def connect(self):
self.redis = await aioredis.create_redis(
'redis://localhost',encoding='utf-8')
async def disconnect(self, close_code):
await self.redis.close()
I need something like self.channel_layer ,using pool connection across every consumer. Thanks.
回答1:
Probably not the best solution, but you can do this
You app's __init__.py
from asgiref.sync import async_to_sync
redis = async_to_sync(aioredis.create_redis)('redis://localhost',encoding='utf-8')
Then in your consumer, you can import and use the existing redis connection
from . import redis
来源:https://stackoverflow.com/questions/56141077/i-want-to-use-aioredis-connection-pool-not-create-new-connection-in-every-async