Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How many instances of app Gunicorn creates

I`m new to this, and misunderstand how Gunicorn + Flask works.

When i run Gunicorn with 4 workers it creates 4 instances of my Flask app, or it will create 4 processes that handle web requests from Nginx and one instance of Flask app?

If i make simple implementation of memory cache(dictionary for example) in my app, will gunicorn create more than one instances of app and therefore more than one instances of cache?

like image 981
Kalyaganov Alexey Avatar asked Jan 19 '16 09:01

Kalyaganov Alexey


People also ask

How many requests can Gunicorn handle?

Yes, with 5 worker processes, each with 8 threads, 40 concurrent requests can be served.

How many Gunicorn workers should I run?

Gunicorn should only need 4-12 worker processes to handle hundreds or thousands of requests per second. Gunicorn relies on the operating system to provide all of the load balancing when handling requests. Generally we recommend (2 x $num_cores) + 1 as the number of workers to start off with.

How many Gunicorn threads are there?

The gunicorn docs suggest that one only needs a small handful of worker "entities" (threads or workers) to handle many thousand requests. As a "sensible" default given the connection limit constraints noted above, we aim for "# workers" x "# threads" to be around 4.

Is Gunicorn multithreaded?

Gunicorn also allows for each of the workers to have multiple threads. In this case, the Python application is loaded once per worker, and each of the threads spawned by the same worker shares the same memory space.


1 Answers

It will create 4 gunicorn workers to handle the one flask app. If you spin 4 instances of a flask app (with docker for example) you will need to run gunicorn 4 times. Finally to handle all those flask instances you will need a Nginx server in front of it acting as a load balancer.

For example, if one user is doing a registration routine that takes a lot of time due to multiple querys to the database you still have another worker to send the request to the flask instance.

I get our point, but Flask is not WSGI ready, which is the stardard. Gunicorn is playing that role in production so you get more reliability instead of using the Develpment standard Werkzeug server that comes with it. In other words, Gunicorn is just a wrapper on you flask object. It just handles the requests and let Flask do its thing.

like image 173
CESCO Avatar answered Sep 19 '22 04:09

CESCO