在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/Java  Python/ flask 官方教程中的create_app是怎么運(yùn)行的?

flask 官方教程中的create_app是怎么運(yùn)行的?

今天在看flask的官方教程在第一部分,創(chuàng)建應(yīng)用這里遇到一個問題,他在__init__.py創(chuàng)建了一個create_app的函數(shù),然后寫了一些配置信息,我并沒有看到他調(diào)用,它是怎么跑起來的?我知道__init__.py這個文件會自動運(yùn)行,但里面的創(chuàng)建的函數(shù)也會自動運(yùn)行嗎?官方代碼如下

mkdir flaskr

flaskr/__init__.py

import os

from flask import Flask


def create_app(test_config=None):
    # create and configure the app
app = Flask(__name__, instance_relative_config=True)
app.config.from_mapping(
    SECRET_KEY='dev',
    DATABASE=os.path.join(app.instance_path, 'flaskr.sqlite'),
)

if test_config is None:
    # load the instance config, if it exists, when not testing
    app.config.from_pyfile('config.py', silent=True)
else:
    # load the test config if passed in
    app.config.from_mapping(test_config)

# ensure the instance folder exists
try:
    os.makedirs(app.instance_path)
except OSError:
    pass

# a simple page that says hello
@app.route('/hello')
def hello():
    return 'Hello, World!'

return app

export FLASK_APP=flaskr
export FLASK_ENV=development
flask run
這樣就跑起來了,但是它是怎么跑起來的create_app是怎么調(diào)用的?
謝謝!

回答
編輯回答
還吻

你可以看看我寫的這篇文章——https://www.os373.cn/article/63

2018年2月2日 19:12
編輯回答
假灑脫

看源碼就行了,執(zhí)行 flask run 的時候會執(zhí)行如下代碼:

def run_command(info, host, port, reload, debugger, eager_loading,
                with_threads, cert):
    debug = get_debug_flag()

    if reload is None:
        reload = debug

    if debugger is None:
        debugger = debug

    if eager_loading is None:
        eager_loading = not reload

    show_server_banner(get_env(), debug, info.app_import_path, eager_loading)
    app = DispatchingApp(info.load_app, use_eager_loading=eager_loading)

    from werkzeug.serving import run_simple
    run_simple(host, port, app, use_reloader=reload, use_debugger=debugger,
               threaded=with_threads, ssl_context=cert)

通過 DispatchingApp 獲取 Flask 實(shí)例,然后運(yùn)行。

Flask.run 的部分代碼:

def run(self, host=None, port=None, debug=None,
            load_dotenv=True, **options):
            ...
            run_simple(host, port, self, **options)

run_command
Flask.run

2018年9月18日 21:25