5

I have a simple web app built with Django & running with Gunicorn with Nginx.

When I open HTOP, I see there are so many processes & threads spawn -- for a single tutorial app that just displays a login form. See screenshot of HTOP below:

enter image description here

Why are there so many of them open for such a simple app?

Here is my configuration

"""gunicorn WSGI server configuration."""
from multiprocessing import cpu_count
from os import environ


def max_workers():
    return cpu_count() * 2 + 1

max_requests = 1000
worker_class = 'gevent'
workers = max_workers()

Thanks

Ricky
  • 2,850
  • 5
  • 28
  • 41

1 Answers1

4

That's because of gunicorn's design:

Gunicorn is based on the pre-fork worker model. This means that there is a central master process that manages a set of worker processes. The master never knows anything about individual clients. All requests and responses are handled completely by worker processes.

That means that gunicorn will spawn as many processes and threads as it needs, depending on the configuration and type of the workers set.

Gustavo Kawamoto
  • 2,665
  • 18
  • 27
  • 1
    Hmmm I guess I still don't understand why there would be so many both processes and workers if it was just a simple app to load a login page? – Ricky Oct 02 '20 at 13:34
  • The gunicorn basically starts many processes to be ready to act as "workers" and handle the requests for the application individually. The `max_workers` function seems to be defining how many will be created (you can test it by returning 1 or 2 instead). – Gustavo Kawamoto Oct 02 '20 at 13:43
  • 1
    Got it, thanks. So I guess they're spawned "just in case". I understand the need for separate threads, but just wondering why multiple processes were added. Perhaps I just need to study more in-depth. Thank you – Ricky Oct 02 '20 at 13:53
  • 2
    Yes, basically. It's not a new concept, `nginx` and `httpd` both uses the same strategy, as it mitigates many risks, such as workers dying, overload, memory limitations and many others. I suggest you looking at [this answer](https://stackoverflow.com/a/25894770/6580047) – Gustavo Kawamoto Oct 02 '20 at 13:57