Because the 0.1 version of the overall code is about 350 lines, relatively simple. Therefore, this article will be based on the flask 0.1 version of the source code to analyze the Flask application startup process.
Flask Reference Flask, the website has one of the simplest apps:
From flask Import Flaskapp = Flask (__name__) @app. Route ('/hello ') def hello_world (): return ' Hello world! ' if __name__ = = ' __main__ ': app.run (host= ' 0.0.0.0 ', port=8080,debug=true)
The following is the simplest of the above Flask app as the starting point, the v0.1 version of the source code as the core to explain the entire flask application startup process:
First, initialize the app
From flask import flask #导入Flask类app = Flask (__name__) #1, instantiate flask app
flask.py file:
1.1. Defining class variables
Class Flask (object): ' Global variable ' ' request_class = Request Response_class = response Static_path = '/static ' Secret_key = None session_cookie_name = ' session ' jinja_options = dict ( autoescape=true, extensions=[' Jinja2.ext.autoescape ', ' jinja2.ext.with_ '] )
1.2. Initialize constructor function
def __init__ (self, package_name): Self.debug = False #debug变量为False self.package_name = package_name #一般为 __name__, __main__ if run as this module, or the app file name if it is called. Self.root_path = _get_package_path (self.package_name) #获取app的绝对路径 self.view_functions = {} #视图函数 Self.erro R_handlers = {} #错误处理 self.before_request_funcs = [] #HTTP请求之前需要执行的函数 self.after_request_funcs = [] #HTT After P request is finished, the function to be executed self.template_context_processors = [_default_template_ctx_processor] #上下文模板变量: Session, G, request Self.url_map = map () #url集合
If Self.static_path is not None: #self. Static_path The default value is '/static ', so it is added to the Url_map collection by default. Map ([<rule '/static/<filename> ', Static>]) Self.url_map.add (Rule (Self.static_path + '/<file Name> ', build_only=true, endpoint= ' static ')) if pkg_resources is not None: target = (self.package_name, ' static ') Else:target = Os.path.join (self.root_path , ' static ') Self.wsgi_app = Shareddatamiddleware (Self.wsgi_app, {self.static_path:target }) self.jinja_env = Environment (Loader=self.create_jinja_loader (), **self.jinja_options) self.jinja_env.globals.update (url_for=url_for, get_flashed_messages=g Et_flashed_messages)
In the constructor, a number of variables (debug, package name, package path, view function, context-sensitive, routing, static path, template-related environment) are mainly defined
Second, the route processing
@app. Route ('/hello ') def hello_world (): return ' Hello world! '
flask.py file
2.1 Defining a route Adorner
The function is to complete initialization of Url_map and view_functions, where rule is the tool provided by Werkzeug.
def route (self, rule, **options): def decorator (f): self.add_url_rule (rule, f.__name__, **options) #Map ( [<rule '/hello ' (head,get), Hello_world>]) SELF.VIEW_FUNCTIONS[F.__NAME__] = f #view_functions = {' Hello_world ': Hello_world} return F return Decorator
def add_url_rule (self, rule, endpoint, **options): options[' endpoint '] = Endpoint options.setdefault (' Methods ', (' GET ',) self.url_map.add (rule (rule, **options))
Third, App.run ()
The first two steps, all of which are initialization operations, are prepared for subsequent launches. Includes initialization environment variables and route additions.
if __name__ = = ' __main__ ': app.run (host= ' 0.0.0.0 ', port=8080,debug=true)
The app is officially launched. The process is as follows:
Flask.run ()- werkzeug.run_simple ()- Werkzeug.inner ()-Werkzeug.serving.make_server () -->serve_forever ()-->socketserver.baseserver.httpserver.serve_forever () #While true:****
At this point, a flask app has run.
Flask Learning-flask App Launch process