Browse Source

redis pub/sub support set password

At first I added it to URL, but it was not easy to handle when the password value was None, so I added a parameter "password"
pull/117/head
larry.liu 8 years ago
committed by GitHub
parent
commit
24b9369c4a
  1. 7
      socketio/asyncio_redis_manager.py

7
socketio/asyncio_redis_manager.py

@ -53,7 +53,7 @@ class AsyncRedisManager(AsyncPubSubManager): # pragma: no cover
name = 'aioredis' name = 'aioredis'
def __init__(self, url='redis://localhost:6379/0', channel='socketio', def __init__(self, url='redis://localhost:6379/0', channel='socketio',
write_only=False): write_only=False, password=None):
if aioredis is None: if aioredis is None:
raise RuntimeError('Redis package is not installed ' raise RuntimeError('Redis package is not installed '
'(Run "pip install aioredis" in your ' '(Run "pip install aioredis" in your '
@ -61,18 +61,19 @@ class AsyncRedisManager(AsyncPubSubManager): # pragma: no cover
self.host, self.port, self.db = _parse_redis_url(url) self.host, self.port, self.db = _parse_redis_url(url)
self.pub = None self.pub = None
self.sub = None self.sub = None
self.password = password
super().__init__(channel=channel, write_only=write_only) super().__init__(channel=channel, write_only=write_only)
async def _publish(self, data): async def _publish(self, data):
if self.pub is None: if self.pub is None:
self.pub = await aioredis.create_redis((self.host, self.port), self.pub = await aioredis.create_redis((self.host, self.port),
db=self.db) db=self.db, password=self.password)
return await self.pub.publish(self.channel, pickle.dumps(data)) return await self.pub.publish(self.channel, pickle.dumps(data))
async def _listen(self): async def _listen(self):
if self.sub is None: if self.sub is None:
self.sub = await aioredis.create_redis((self.host, self.port), self.sub = await aioredis.create_redis((self.host, self.port),
db=self.db) db=self.db, password=self.password)
self.ch = (await self.sub.subscribe(self.channel))[0] self.ch = (await self.sub.subscribe(self.channel))[0]
while True: while True:
return await self.ch.get() return await self.ch.get()

Loading…
Cancel
Save