码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2017-09-19 23:01:16      阅读:268      评论:0      收藏:0      [点我收藏+]

标签:proc   oca   cat   使用   repr   bsp   on()   inner   hostname   

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源代码笔记——应用启动

标签:proc   oca   cat   使用   repr   bsp   on()   inner   hostname   

原文地址:http://www.cnblogs.com/tdkihrr/p/7554389.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!