【问题标题】:How can I run this flask app using gunicorn如何使用 gunicorn 运行这个烧瓶应用程序
【发布时间】:2020-05-23 00:47:24
【问题描述】:

我在编写代码后重构了我的代码,结果证明这是一个错误,现在我无法使用 gunicorn 运行它。如果您有关于从这里开始构建或分解的建议,那么也欢迎您。 我正在努力让它从现在开始运行 5 天,挣扎了很多。

目录结构

app--
     --db.sqlite
     --__init__.py
     --models.py
     --MyFunctions.py
     --routes.py
     --schemas.py
     --sockets.py
app.py

在 app.py 中

from app import *


from app.MyFunctions import *
from app.models import *
from app.schemas import *
from app.routes import *
from app.sockets import *


port=os.environ.get("PORT")

if port is None or port == "":
    port = 3000

if __name__ == '__main__':
    socketio.run(app, host='0.0.0.0', port=port) # when running on heroku

在init.py

里面
import jwt
from flask import (
    Flask,
    request,
    jsonify,
    make_response,
)
from flask_socketio import (
    SocketIO, 
    send, 
    emit, 
    join_room, 
    leave_room,
)
from sqlalchemy import (
    Column,
    Integer,
    String,
    Boolean,
    ForeignKey,
    DateTime,
    Sequence,
    Float,
)
from flask_sqlalchemy import SQLAlchemy
from flask_marshmallow import Marshmallow
from os import environ


def debug(msg):
    print('\n\n\n'+str(msg)+'\n\n\n'+'\n\nType:\t'+str(type(msg))+'\n\n')

from .models import *
socketio = SocketIO(app, cors_allowed_origins='*')

在models.py里面(只导入相关的东西不导入模型,我的大部分导入都在这里)

from flask import (
    Flask,
    request,
    jsonify,
    make_response,
    redirect,
)
from flask_socketio import (
    SocketIO, 
    send, 
    emit, 
    join_room, 
    leave_room,
)
from sqlalchemy import (
    Column,
    Integer,
    String,
    Boolean,
    ForeignKey,
    DateTime,
    Sequence,
    Float,
)
from flask_sqlalchemy import SQLAlchemy
from flask_marshmallow import Marshmallow
from flask_bcrypt import Bcrypt
from os import environ
from flask_cors import CORS, cross_origin



app = Flask(__name__)
root = os.path.dirname(os.path.abspath(__file__))
CORS(app, support_credentials=True)
basedir = os.path.abspath(os.path.dirname(__file__))

PG = os.environ.get("DATABASE_URL")

if PG is None or PG=="":
    app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + \
        os.path.join(basedir, 'db.sqlite')
else:
    app.config['SQLALCHEMY_DATABASE_URI'] = PG


app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SECRET_KEY'] = environ.get('STRIPE_API_KEY')


db = SQLAlchemy(app)

ma = Marshmallow(app)

bcrypt = Bcrypt(app)

socketio = SocketIO(app, cors_allowed_origins='*')

编辑: 目前我使用 python 仅在 heroku 上运行应用程序,所以我的 Procfile 看起来像

web: python app.py

是的,我尝试了许多其他格式,例如

web: gunicorn app:app
web: gunicorn models:app
web: gunicorn app/models:app

尝试通过制作不同的运行文件和许多其他方法 恐怕我需要改变它的结构才能让它发挥作用。

【问题讨论】:

  • 更深入地了解 Heroku、gunicorn 和 Flask:medium.com/@gitaumoses4/…
  • 你运行的哪个 gunicorn 命令不起作用?
  • 请告诉我们您的Procfile。
  • 另外,为了在 heroku 中运行你的 python 应用程序,需要在项目的根目录中声明 requirements.txt 文件。 gunicorn 也必须出现在那里。

标签: python heroku flask hosting gunicorn


【解决方案1】:

我重命名了我的 app.py 文件,然后它工作了

Procfile 内部

web: gunicorn gettingstarted.wsgi
web: gunicorn runner:app

【讨论】:

    猜你喜欢
    • 2012-11-19
    • 2021-01-18
    • 2015-07-05
    • 1970-01-01
    • 2019-04-10
    • 2018-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多