Created
April 27, 2020 01:28
-
-
Save Andrew-Chen-Wang/fceb9ccd022f820789f5fb7d8932697d to your computer and use it in GitHub Desktop.
Redis Handling using three connections max for Django Websocket Applications using Pub/Sub
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Import RedisSingleton and create the object by doing: | |
redis_singleton = await RedisSingleton() | |
That way, you've not only created a new object in memory but also | |
executed the await in the magic method. This is helpful when you have | |
wayyy too many Django's and connections become overloaded. | |
There should now only be three connection for: | |
PUBSCRIBE x2 and PUBLISH | |
id=8 addr=[::1]:61841 fd=9 name= age=26 idle=4 flags=P db=0 sub=0 psub=1 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=psubscribe | |
id=9 addr=[::1]:61842 fd=10 name= age=26 idle=4 flags=P db=0 sub=0 psub=1 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=psubscribe | |
id=10 addr=[::1]:61843 fd=11 name= age=24 idle=4 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=publish | |
(Note: if you do CLIENT LIST in the redis cli, then the first conn is most | |
likely your cli conn to Redis, not Django instance's. Notice the ids). | |
Tested on 3 different users. | |
""" | |
import aioredis | |
class RedisSingleton(object): | |
# docker-compose -f local.yml run --rm redis redis-cli -u redis://redis:6379/ | |
_instance = None | |
_initial = False | |
redis = 0 | |
ch = 0 | |
async def initialize_redis(self): | |
redis = await aioredis.create_redis_pool('redis://localhost:6379') | |
assert type(redis) is not None, "Couldn't connect to Redis from RedisSingleton" | |
ch, = await redis.psubscribe('chat_*') | |
return redis, ch | |
def __await__(self): | |
if not self._initial: | |
self.redis, self.ch = yield from self.initialize_redis().__await__() | |
self._initial = True | |
return self._instance | |
def __new__(cls): | |
if not cls._instance: | |
print('Creating RedisSingleton') | |
cls._instance = super(RedisSingleton, cls).__new__(cls) | |
return cls._instance |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment