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
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.