flask源代码笔记——应用启动

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了flask源代码笔记——应用启动相关的知识,希望对你有一定的参考价值。

flask一个最简单的demo是:

from flask import Flask

app = Flask(__name__)

@app.route(/)
def index():
    return Hello, World!

if __name__ == __main__:
    app.run()

run()方法启动了应用,那么run()背后都调用哪些类、方法和函数呢?

将相关代码汇总起来,如下:

def run(self, host=localhost, port=5000, **options):
        from werkzeug import run_simple
        if debug in options:
            self.debug = options.pop(debug)
        options.setdefault(use_reloader, self.debug)
        options.setdefault(use_debugger, self.debug)
        return run_simple(host, port, self, **options)

def run_simple(hostname, port, application, use_reloader=False,
               use_debugger=False, use_evalex=True,
               extra_files=None, reloader_interval=1, threaded=False,
               processes=1, request_handler=None, static_files=None,
               passthrough_errors=False, ssl_context=None):
    def inner():
        make_server(hostname, port, application, threaded,
                    processes, request_handler,
                    passthrough_errors, ssl_context).serve_forever()

def make_server(host, port, app=None, threaded=False, processes=1,
                request_handler=None, passthrough_errors=False,
                ssl_context=None):
    if threaded and processes > 1:
        raise ValueError("cannot have a multithreaded and "
                         "multi process server.")
    elif threaded:
        return ThreadedWSGIServer(host, port, app, request_handler,
                                  passthrough_errors, ssl_context)
    elif processes > 1:
        return ForkingWSGIServer(host, port, app, processes, request_handler,
                                 passthrough_errors, ssl_context)
    else:
        return BaseWSGIServer(host, port, app, request_handler,
                              passthrough_errors, ssl_context)

class BaseWSGIServer(HTTPServer, object):
    def __init__(self, host, port, app, handler=None,
                 passthrough_errors=False, ssl_context=None):
        if handler is None:
            handler = WSGIRequestHandler
        self.address_family = select_ip_version(host, port)
        HTTPServer.__init__(self, (host, int(port)), handler)
        self.app = app
        self.passthrough_errors = passthrough_errors

# BaseWSGIServer第四行
class WSGIRequestHandler(BaseHTTPRequestHandler, object):
    def run_wsgi(self):
        app = self.server.app
        environ = self.make_environ()
        headers_set = []
        headers_sent = []

        def execute(app):
            application_iter = app(environ, start_response)

# 调用了__call__方法,所以要回到Flask类中寻找相关代码
class Flask(object):
    def __call__(self, environ, start_response):
        return self.wsgi_app(environ, start_response)

    def wsgi_app(self, environ, start_response):
        with self.request_context(environ):
            rv = self.preprocess_request()
            if rv is None:
                rv = self.dispatch_request()
            response = self.make_response(rv)
            response = self.process_response(response)
            return response(environ, start_response)

使用graphviz将关系画出来,如下:
技术分享

 

以上是关于flask源代码笔记——应用启动的主要内容,如果未能解决你的问题,请参考以下文章

Flask源码实现(0.1版本)笔记

flask源代码笔记——路由

Flask 源代码阅读笔记

Android - 应用程序启动时片段 onCreate 崩溃

flask源代码笔记——路由

为啥不每次 Flask 启动时生成密钥?