How to request Gunicorn for a flask

Can someone describe the process of switching Gunicorn to Flask internally ?

It would be great if someone explained every step involved in the process, from receiving a request from Gunicorn to forward it to Flask and vice versa.

Please keep in mind when explaining that I am new to this area.

+5
source share
1 answer

Gunicorn and Flask discuss WSGI , which has two sides: server and application.

on the application side (framework), we need to provide a callable, simplest example:

 def application(environ, start_response): start_response('200 OK', [('Content-Type', 'text/plain')]) return ['Hello World'] 

the server will call this application and provide environmental information and a callback function, which is used to indicate the beginning of the response. when the server receives a response, it will return it to the browser.

so, for guns and flasks:

 from flask import Flask app = Flask(__name__) 

when you do this, you really have a WSGI-compatible application, the app is callable:

 class Flask(object): ... def __call__(self, environ, start_response): """Shortcut for :attr:`wsgi_app`.""" return self.wsgi_app(environ, start_response) [source](https://github.com/mitsuhiko/flask/blob/master/flask/app.py#L1976) 

and when you run gunicorn app:app , you tell gunicorn where to download your application, source

when a request arrives, gunicorn parses it, constructs a dict environ , which is defined here , contains information like REQUEST_METHOD , QUERY_STRING , etc. then call the application (Flask object!) with it: app(environ, start_repsonse) source , start_repsonse is a callback in Gunicorn to get the response status and headers, and the return value of the app call will be sent as the response body.

+9
source

Source: https://habr.com/ru/post/1244583/


All Articles