D
D
Dmitry2017-09-22 16:15:44
Django
Dmitry, 2017-09-22 16:15:44

How to properly set up uWSGI for Django?

I ran into a problem - mail is not sent from the forum site ( here is the question ).
More precisely, it is sent only to me and sometimes to others. Prescribed SPF, DKIM, etc. I send through a kind of "reliable" service pdd.yandex.ru.
I started testing through mail-tester.com. I wrote the testmail command for ./manage.py and I'm playing from the console - all letters reach the mail tester, give out 10 points out of 10, i.e. the letter is perfect!
So, they get it from the console, but from the server - nginx -> uwsgi -> django - Well, no way. Django happily reports back with the "Your mail has been sent" form, but nothing comes to the mail tester.
I started digging into the sources for sending mail in Django and came across this thing:

class EmailBackend(BaseEmailBackend):
    """
    A wrapper that manages the SMTP network connection.
    """
    def __init__(self, ...):
        # ... скипаем все и в самом конце - вот:
        self._lock = threading.RLock()

    def send_messages(self, email_messages):
        """
        Sends one or more EmailMessage objects and returns the number of email
        messages sent.
        """
        if not email_messages:
            return
        with self._lock:
            # ... ну и дальше, собственно, отправляет.
        return num_sent

Those. what does it get? Django, it turns out, creates a separate thread for sending mail via SMTP? Explain, please!
I read (but did not fully understand) that in uWSGI you need to somehow separately configure the correct work with threads, otherwise the workers lose contact with the spawned threads and errors may occur because of this.
I have included this in my config for uWSGI:
[uwsgi]
...
master = true
processes = 4
enable-threads = true
single-interpreter = true
...

As a result, in htop I see 4 workers, each of which made one thread (it was also played with 2 threads). It seems that streams are enabled ... But the stream with sending mail to Django either does not start or is dying (probably), because mail is not sent. More precisely, it is sent to me (for some reason) to all available mailboxes, but it doesn’t come to the mail tester :(
Please explain about django + uwsgi + threads.

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