Answer the question
In order to leave comments, you need to log in
Django + UWSGI + Redis pub-sub - how to start this business correctly?
Hello everyone!)
In our django application, we use Redis pubsub, so that our two independent django applications can communicate with each other. For pubsub to work, we need to run the subscriber in the application that will listen to the channels. To do this, we run our radish thread daemon in wsgi.py.
from django.conf import settings
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
if settings.START_DELETE_SUBSCRIBER:
from app1.handlers import App1Subscriber
App1Subscriber().start()
[uwsgi]
http = 0.0.0.0:8000
chdir = /opt/app1
module = django_project.wsgi:application
master = True
cheaper = 2
processes = 16
harakiri = 70
max-requests = 5000
vacuum = True
reaper = True
enable-threads = True
single-interpreter = True
class App1Subscriber(BasePubSub, metaclass=abc.ABCMeta):
redis = StrictRedis(settings.REDIS_HOST, settings.REDIS_PORT)
thread = None
pubsub = None
handler = None
channel = None
def __init__(self):
self.pubsub = self.redis.pubsub()
self.channel = **some_channel**
self.handler = **some_handler**
def start(self, daemon=True):
if self.thread:
raise RuntimeError('Subscriber is already running')
sub = getattr(self.pubsub, 'subscribe')
sub(**{self.channel: self.handler})
self.thread = self.pubsub.run_in_thread(daemon=daemon)
def stop(self):
if not self.thread:
raise RuntimeError('Subscriber is not running')
self.thread.stop()
@property
def running(self):
return self.thread.is_alive()
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question