使用 gunicorn 部署 Flask 应用程序 (exploreflask tuto)

Deploy a Flask app with gunicorn (exploreflask tuto)

为了了解如何部署 Flask 应用程序,我阅读了一些教程并发现 exploreflask 中的一个非常好。

我将 blueprints-functional structure to build my test-app, BUT at the end, at the deployment part 与 gunicorn 等应用程序运行器一起使用,tuto 使用了一个具有唯一文件的示例,与其他文件完全不同

arandomfoldername/
    config.py
    requirements.txt
    run.py
    instance/
      config.py
    myappname/
        __init__.py
        static/
        templates/
            home/
            control/
        views/
            __init__.py
            home.py
            control.py
        models.py

# myappname/__init__.py
from flask import Flask
from .views.home import bluehome 
from .views.control import bluecontrol 

app = Flask(__name__, instance_relative_config=True)
app.register_blueprint(bluehome)
app.register_blueprint(bluecontrol)

所以我尝试了 gunicorn myappname:app 但我得到了 No module named myappnameapp 变量在 myappname 包的 __init_.py 中(作为 tuto显示在蓝图部分)

我用这个 flask tuto 构建了 wheel 文件

  1. 如何管理和解决这个问题?

  2. 顶级文件 config.py 中给出的参数应该如何使用,因为它们未在 wheel 文件中使用?

在一些教程中,比如flask.pocoo它说要构建一个wheel文件并安装它,但是gunicorn不使用这种方式。

  • 使用 Factory 模式
  • 使用以 ex wsgi.py 命名的文件从工厂导入 app

然后将整个文件夹复制到生产环境中:

arandomfoldername/
    config.py
    requirements.txt
    run.py
    instance/
      config.py
    myappname/
        __init__.py
        wsgi.py
        static/
        templates/
            home/
            control/
        views/
            __init__.py
            home.py
            control.py
        models.py

然后

cd arandomfoldername
gunicorn myappname.wsgi:app