11import asyncio
2- import logging
32import pickle
43from urllib .parse import urlparse
54
109
1110from .asyncio_pubsub_manager import AsyncPubSubManager
1211
13- logger = logging .getLogger ('socketio' )
14-
1512
1613def _parse_redis_url (url ):
1714 p = urlparse (url )
@@ -52,15 +49,15 @@ class AsyncRedisManager(AsyncPubSubManager): # pragma: no cover
5249 name = 'aioredis'
5350
5451 def __init__ (self , url = 'redis://localhost:6379/0' , channel = 'socketio' ,
55- write_only = False ):
52+ write_only = False , logger = None ):
5653 if aioredis is None :
5754 raise RuntimeError ('Redis package is not installed '
5855 '(Run "pip install aioredis" in your '
5956 'virtualenv).' )
6057 self .host , self .port , self .password , self .db = _parse_redis_url (url )
6158 self .pub = None
6259 self .sub = None
63- super ().__init__ (channel = channel , write_only = write_only )
60+ super ().__init__ (channel = channel , write_only = write_only , logger = logger )
6461
6562 async def _publish (self , data ):
6663 retry = True
@@ -74,11 +71,13 @@ async def _publish(self, data):
7471 pickle .dumps (data ))
7572 except (aioredis .RedisError , OSError ):
7673 if retry :
77- logger .error ('Cannot publish to redis... retrying' )
74+ self ._get_logger ().error ('Cannot publish to redis... '
75+ 'retrying' )
7876 self .pub = None
7977 retry = False
8078 else :
81- logger .error ('Cannot publish to redis... giving up' )
79+ self ._get_logger ().error ('Cannot publish to redis... '
80+ 'giving up' )
8281 break
8382
8483 async def _listen (self ):
@@ -92,8 +91,9 @@ async def _listen(self):
9291 self .ch = (await self .sub .subscribe (self .channel ))[0 ]
9392 return await self .ch .get ()
9493 except (aioredis .RedisError , OSError ):
95- logger .error ('Cannot receive from redis... '
96- 'retrying in {} secs' .format (retry_sleep ))
94+ self ._get_logger ().error ('Cannot receive from redis... '
95+ 'retrying in '
96+ '{} secs' .format (retry_sleep ))
9797 self .sub = None
9898 await asyncio .sleep (retry_sleep )
9999 retry_sleep *= 2
0 commit comments