我正在开发一个可以正常工作的Flask API。我现在正试图用uWSGI替换Flask开发服务器,但一切都中断了。我试图解决这个问题,因为2天,经过教程并在这里搜索但是找不到问题的解决方案。这是代码: 的 app.py
import logging.config
import settings
import utils
from flask import Flask, Blueprint
from flask_restplus import Resource, Api
from flask_cors import CORS
from api.restplus import api
from api.gan.endpoints.client import ns as gan_client_namespace
# create Flask application
app = Flask(__name__)
CORS(app) # needed for cross-domain requests, allow everything by default
# load logging confoguration and create log object
logging.config.fileConfig('logging.conf')
log = logging.getLogger(__name__)
#
@app.route("/")
def hello():
return "Hello World!"
def __get_flask_server_params__():
'''
Returns connection parameters of the Flask application
:return: Tripple of server name, server port and debug settings
'''
server_name = utils.get_env_var_setting('FLASK_SERVER_NAME', settings.DEFAULT_FLASK_SERVER_NAME)
server_port = utils.get_env_var_setting('FLASK_SERVER_PORT', settings.DEFAULT_FLASK_SERVER_PORT)
flask_debug = utils.get_env_var_setting('FLASK_DEBUG', settings.DEFAULT_FLASK_DEBUG)
flask_debug = True if flask_debug == '1' else False
return server_name, server_port, flask_debug
def configure_app(flask_app, server_name, server_port):
'''
Configure Flask application
:param flask_app: instance of Flask() class
'''
flask_app.config['SERVER_NAME'] = server_name + ':' + server_port
flask_app.config['SWAGGER_UI_DOC_EXPANSION'] = settings.RESTPLUS_SWAGGER_UI_DOC_EXPANSION
flask_app.config['RESTPLUS_VALIDATE'] = settings.RESTPLUS_VALIDATE
flask_app.config['RESTPLUS_MASK_SWAGGER'] = settings.RESTPLUS_MASK_SWAGGER
flask_app.config['ERROR_404_HELP'] = settings.RESTPLUS_ERROR_404_HELP
def initialize_app(flask_app, server_name, server_port):
'''
Initialize Flask application with Flask-RestPlus
:param flask_app: instance of Flask() class
'''
blueprint = Blueprint('tf_api', __name__, url_prefix='/tf_api')
configure_app(flask_app, server_name, server_port)
api.init_app(blueprint)
api.add_namespace(gan_client_namespace)
flask_app.register_blueprint(blueprint)
#from werkzeug.contrib.fixers import ProxyFix
#flask_app.wsgi_app = ProxyFix(flask_app.wsgi_app)
def main():
server_name, server_port, flask_debug = __get_flask_server_params__()
initialize_app(app, server_name, server_port)
log.info(
'>>>>> Starting TF Serving client at http://{}/ >>>>>'.format(app.config['SERVER_NAME'])
)
app.run(debug=flask_debug, host=server_name)
if __name__ == '__main__':
main()
当我使用python app.py
运行时,它运行正常。
现在我只想尝试使用uWSGI获得相同的结果:
uwsgi --http :8000 --wsgi-file app.py --callable app
命令行消息看起来很好:
访问0.0.0.0:8000也可以
但是,当我尝试访问API时,它失败了: 0.0.0.0:8000/tf_api
我已经尝试了一些我想到的但却无法解决这个问题。我做的一些事情是:
包含以下代码:
来自werkzeug.contrib.fixers导入ProxyFix flask_app.wsgi_app = ProxyFix(flask_app.wsgi_app)
删除app.run()
(Flask Gunicorn app can't get __name__ to equal '__main__')
将function_calls移到main函数之外,否则只有在直接运行app.py时才会调用它们,类似于(Flask application with Blueprints+uWSGI+nginx returning 404's (no routing?))
我真的不知道还有什么问题。我发现了一个类似的问题,但它不适用于我的情况,因为它与我尚未使用的NGINX有关(How to deploy a flask-restplus app with nginx + uwgsi)。任何想法我还能尝试什么?也许我错过了一些显而易见的东西,因此没有多少睡觉。
答案 0 :(得分:2)
wsgi服务器看不到initialize_app内部的所有内容,
import logging.config
import settings
import utils
from flask import Flask, Blueprint
from flask_restplus import Resource, Api
from flask_cors import CORS
from api.restplus import api
from api.gan.endpoints.client import ns as gan_client_namespace
# create Flask application
app = Flask(__name__)
CORS(app) # needed for cross-domain requests, allow everything by default
# load logging confoguration and create log object
logging.config.fileConfig('logging.conf')
log = logging.getLogger(__name__)
#
@app.route("/")
def hello():
return "Hello World!"
server_name = utils.get_env_var_setting('FLASK_SERVER_NAME', settings.DEFAULT_FLASK_SERVER_NAME)
server_port = utils.get_env_var_setting('FLASK_SERVER_PORT', settings.DEFAULT_FLASK_SERVER_PORT)
flask_debug = utils.get_env_var_setting('FLASK_DEBUG', settings.DEFAULT_FLASK_DEBUG)
flask_debug = True if flask_debug == '1' else False
app.config['SERVER_NAME'] = server_name + ':' + server_port
app.config['SWAGGER_UI_DOC_EXPANSION'] = settings.RESTPLUS_SWAGGER_UI_DOC_EXPANSION
app.config['RESTPLUS_VALIDATE'] = settings.RESTPLUS_VALIDATE
app.config['RESTPLUS_MASK_SWAGGER'] = settings.RESTPLUS_MASK_SWAGGER
app.config['ERROR_404_HELP'] = settings.RESTPLUS_ERROR_404_HELP
blueprint = Blueprint('tf_api', __name__, url_prefix='/tf_api')
api.init_app(blueprint)
api.add_namespace(gan_client_namespace)
flask_app.register_blueprint(blueprint)
def main():
log.info(
'>>>>> Starting TF Serving client at http://{}/ >>>>>'.format(app.config['SERVER_NAME'])
)
app.run(debug=flask_debug, host=server_name)
if __name__ == '__main__':
main()
答案 1 :(得分:0)
您正在wsgi服务器永远不会看到的函数中调用app.register_blueprint。
我猜你已经遵循某种指南,这些指南为你配置/初始化等应用程序提供了很好的功能。 但它纯粹是为开发服务器所做的。
您必须在函数外部调用Blueprint和init_app,或者创建一个导入app的新wsgi.py文件,然后运行Blueprint和api.init_app命令。