简体   繁体   中英

How Gunicorn forward request to flask

Can anyone describe the process of how Gunicorn forward the request to Flask internally ?

It would be great if someone explains each and every step involved in the process from receiving the request by Gunicorn to forwarding it to Flask and the reverse way.

Please keep in mind while explaining that I am a newbee in this area.

Gunicorn and Flask talks through WSGI , which has two sides: server side and the application side.

on the application(framework) side, we need to provide a callable, the 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 providing environment information and a callback function which is used to indicate start of a response. when the server get the response, it will return it to browser.

so, for gunicorn and flask:

from flask import Flask
app = Flask(__name__)

when you do this, you've actually got an WSGI compatible application, app is a 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're telling gunicorn where to load your application, source

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM