问题描述
我使用flask_appbuilder 构建了一个应用程序。我正在尝试使用 gunicorn 为应用程序提供服务,但出现此错误:
$ gunicorn app:app
[2021-05-25 09:18:40 +0300] [1211266] [INFO] Starting gunicorn 20.1.0
[2021-05-25 09:18:40 +0300] [1211266] [INFO] Listening at: http://127.0.0.1:8000 (1211266)
[2021-05-25 09:18:40 +0300] [1211266] [INFO] Using worker: sync
[2021-05-25 09:18:40 +0300] [1211276] [INFO] Booting worker with pid: 1211276
2021-05-25 09:18:49,374:ERROR:flask_appbuilder.security.sqla.manager:DB Creation and initialization Failed: Can't load plugin: sqlalchemy.dialects:None
应用文件看起来像这样
初始化.py 导入日志
from flask import Flask,request
from flask_graphql import GraphQLView
from flask_appbuilder import AppBuilder,sqlA
from .gql_schema import schema
"""
Logging configuration
"""
logging.basicConfig(format="%(asctime)s:%(levelname)s:%(name)s:%(message)s")
logging.getLogger().setLevel(logging.DEBUG)
app = Flask(__name__)
app.config.from_object("config")
db = sqlA(app)
appbuilder = AppBuilder(app,db.session)
from . import models,views,gql_schema,api
app.add_url_rule(
"/graphql",view_func=GraphQLView.as_view("graphql",schema=schema,graphiql=True)
)
和 cconfig.py
import os
from flask_appbuilder.security.manager import (
AUTH_OID,AUTH_REMOTE_USER,AUTH_DB,AUTH_LDAP,AUTH_OAUTH,)
basedir = os.path.abspath(os.path.dirname(__file__))
# Your App secret key
SECRET_KEY = "my secret key"
DB_ENGINE = os.environ.get('DB_ENGINE')
DB_USER = os.environ.get('DB_USER')
DB_PASSWORD = os.environ.get('DB_PASSWORD')
DB_NAME = os.environ.get('DB_NAME')
DB_HOST = os.environ.get('DB_HOST')
sqlALCHEMY_DATABASE_URI = f'{DB_ENGINE}://{DB_USER}:{DB_PASSWORD}@{DB_HOST}/{DB_NAME}'
sqlALCHEMY_TRACK_MODIFICATIONS = False
FAB_API_SWAGGER_UI = True
FAB_API_SHOW_STACKTRACE = True
可能是什么问题?
解决方法
我想出了问题所在。使用 gunicorn 或 flask_twisted 启动应用程序时数据库连接的 .env 变量,其中返回 None 因此错误。
我必须安装 python_environ 才能支持 python_dotenv。
我正在使用flask-twisted 来运行我的应用程序。这是我添加的内容。
用于运行应用程序的 server.py 文件。
from twisted.application.service import Application
from app import twisted
PORT = 8010
application = Application('twisted-flask')
twisted.run(run_reactor=False,port=PORT)
用于启动应用程序的 bash (start.sh) 文件
#!/bin/bash
echo "----- Starting Application -----"
source venv/bin/activate
path=${PWD}
cd $path
command pip install -r $path/requirements.txt
#
export PYTHONPATH=$PATH:$path
export CDLP_SETTINGS_MODULE=config.Config
twistd -y server.py