如何在 Flask 中使用 Gunicorn

How to use Gunicorn with Flask

我正在尝试在 Heroku 上部署我的 Flask 应用程序,但我遇到了 Procfile 问题。 我正在阅读一篇文章,其中包含此文件。

app.py

from flask import Flask

app = Flask(__name__)

@app.route("/")
def home():
    return "<h1>Hello, World</h1>"

然后在Procfile中是这样的:

web: gunicorn app:app

它起作用了,但事实并非如此,我的文件夹结构是这样的:
flask_simple_app文件夹

├── run.py
└── simple_app
    ├── __init__.py
    ├── routes.py
    ├── static
    │   └── style.css
    └── templates
        ├── base.html
        └── home.html

run.py 包含以下行:


from simple_app import app

if __name__ == '__main__':
    app.run()

init.py 包含以下行:


from flask import Flask

app = Flask(__name__)

from simple_app import routes

routes.py 包含以下行:


from simple_app import app
from flask import render_template

@app.route("/")
def home():
    return render_template('home.html')

所以我的问题是知道我应该把 Procfile 放在哪里以及我将如何写这个:

web: gunicorn ...:...

应该是:

  • web: gunicorn module_name_where_app_instance_exists:name_of_the_app_instance

你的情况:

  • web: gunicorn run:app