I
I
Idobrodushniy2018-06-21 13:13:12
Django
Idobrodushniy, 2018-06-21 13:13:12

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()

We start the application in this way: uwsgi --ini app1_uwsgi_config.ini
Contents of the uwsgi file:
[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

The whole problem is that sometimes the uwsgi server simply does not give any response after launch, and sometimes everything is ok. It doesn't throw any warnings or errors to the console, it just starts up, but no response comes from host port 8000.
Here is the implementation of the subscriber itself:
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()

What could be the reason for this behaviour?
PS I wrote a question so as not to hear about the fact that you do not need to use a pubsub, but because I want to understand what the problem is with such an implementation and that I do not know about the behavior of UWSGI.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question