I want to use aioredis connection pool ,not create new connection in every AsyncWebsocketConsumer.connect .Where can I put the code

流过昼夜 提交于 2021-01-29 05:50:55

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!