D
D
Deadkenny2016-01-20 14:41:42
Flask
Deadkenny, 2016-01-20 14:41:42

How many Flask app instances does Gunicorn run?

On my server, I have Nginx running, which sends requests to Gunicorn, which sends them further to Flask. Gunicorn is running in multiple processes.
And I don't quite understand how Gunicorn works. Does it run one instance of my Flask app, or does it run one instance per process? Or maybe Flask is somehow cleverly launched and creates a new thread for each of the processes that launched it?
I have created a blocking request in which to process a 20 second wait and then return a response. And the application, with 2 running Gunicorn processes, allowed 8 requests to be made, and the 9th request was queued.
The Flask documentation says the following:

However, Flask is just not designed for large applications or asynchronous servers. Flask wants to make it quick and easy to write a traditional web application.

Which explains that the Flask application itself is not asynchronous.

Answer:
In the end, I came to the conclusion that Gunicorn runs one instance of the Flask application.
Requests from NGINX get to Gunicorn, and it in turn decides to pass them to the application now or later. Requests from Gunicorn go to Flask's wsgi_app object. For each of the requests, the Flask application allocates a thread from the thread pool with its own context, and after the request, this context kills. Thus, it turns out that each Gunicorn worker is processed in one instance of the application asynchronously.

Standalone WSGI Containers
Middlewares Werkzeug
Flask Wikibooks

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Deadkenny, 2016-01-22
@Deadkenny

In the end, I came to the conclusion that Gunicorn runs one instance of the Flask application.
Requests from NGINX get to Gunicorn, and it in turn decides to pass them to the application now or later. Requests from Gunicorn go to Flask's wsgi_app object. For each of the requests, the Flask application allocates a thread from the thread pool with its own context, and after the request, this context kills. Thus, it turns out that each worker Gunicorn is processed in one instance of the application asynchronously.
Standalone WSGI Containers
Middlewares Werkzeug
Flask Wikibooks

R
Roman_Kh, 2016-01-20
@Roman_Kh

The number of processes gunicorn starts is determined by its workers parameter .
However, with the help of "asynchronous" workers (for example, gevent ) it is possible to conditionally process several requests in parallel even in one process.
The worker's type is set using the worker-class parameter .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question