无法从uwsgi加载配置

2024-06-08 00:17:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我的Python应用程序中有以下设置

服务器.py

from bots.flask_app import app
from bots.flask_app.api import api
from bots.flask_app.public import public
from bots import db

from bots.commons.helpers.flask.json.serializer import make_alternative_encoder

from flask_debugtoolbar import DebugToolbarExtension

import logging

import bots.commons.managers.configuration as ConfigurationManager

logger = logging.getLogger()


#######
# Public functions
#######
def setup_db_and_app():
    # Flask application bootstrap
    config = ConfigurationManager.get_flask_rest_config()
    app.config.update(config)
    logger.debug('Flask configuration object: %s', app.config)

    # MongoDB connection initialization
    db.init_app(app)

    # Debug toolbar enabled only if Flask in debug mode
    if ConfigurationManager.get_raw_flask_rest_config()['flask']['debug']:
        DebugToolbarExtension(app)

    # Replace the serializer with the custom one (for ObjectId and DateTime serialization)
    app.json_encoder = make_alternative_encoder(app.json_encoder)

    # Register the components
    app.register_blueprint(api)
    app.register_blueprint(public)


def start_server():
    setup_db_and_app()
    logger.debug('Registered routes: %s', app.url_map)
    app.run(host='0.0.0.0')

主.py

import bots.flask_app.server as FlaskApp

import bots.commons.managers.log as LogManager

# Logging initialization
LogManager.init_logging()

# Defined in server.py
FlaskApp.start_server()

我想看看这个敷贴器是否可以由uwsgi提供如下服务

uwsgi --socket 0.0.0.0:8080 --protocol=http -w main

输出如下

INFO:werkzeug: * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
INFO:werkzeug: * Restarting with stat
unable to load configuration from uwsgi

我的问题 一。在哪里可以找到导致此问题的配置? 2。main.py是否可以定义为可调用并用作-w的参数?

这是一个已经有人编写的应用程序,我正在尝试通过uwsgi提供这个应用程序。

任何建议都会有帮助的

谢谢


Tags: frompydebugimportapiconfigapp应用程序
1条回答
网友
1楼 · 发布于 2024-06-08 00:17:58

我也有“无法从uwsgi加载配置”错误。根据flask uwsgi docs

Please make sure in advance that any app.run() calls you might have in your application file are inside an if __name__ == '__main__': block or moved to a separate file. Just make sure it’s not called because this will always start a local WSGI server which we do not want if we deploy that application to uWSGI.

我把app.run()移到if __name__ == '__main__':,问题就解决了。也许你可以试着把FlaskApp.start_server()放在if __name__ == '__main__':下面。

相关问题 更多 >

    热门问题