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

369 views
Skip to first unread message

CrazyNPC

unread,
May 13, 2019, 10:57:30 PM5/13/19
to Django users
Hello,
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.

Nathan Jones

unread,
Feb 4, 2020, 8:03:34 PM2/4/20
to Django users
Hi,

Did you ever work out how to do this?

The only way I could work out how to use a shared pool was to go back to the regular sync `redis` library and use sync_to_async, for example

`settings.py`
REDIS_PRO_POOL = redis.ConnectionPool(host='localhost', port=6379, db=3)

`consumer.py`
class TestConsumer(AsyncWebsocketConsumer):

   
@sync_to_async
    def test(self, value):
        r
= redis.Redis(connection_pool=settings.REDIS_PRO_POOL)
        r
.set("TEST", value)
       
print(r.get(value))
        r
.close()

   
async def connect(self):
       
await self.test("hello")  
       
....


Reply all
Reply to author
Forward
0 new messages