如何使用 gunicorn 运行封装在类中的 Flask 服务器

问题描述 投票:0回答:0

我在使用 guncicorn 运行 Flask 服务器时遇到问题,其中服务器代码包装在一个类中。

这是我的服务器类:

class WebServer:

    def __init__(self):
        self.app = Flask(__name__)
        self.generateURL()
       
    def generateURL(self):
        self.app.route("/")(self.test)
        self.app.route('/weight', methods=['GET'])(self.receive_weight)
        self.app.route('/usage', methods=['GET'])(self.receive_usage)

   ... 

    def run(self):
        self.app.run(debug=True, host="0.0.0.0", port=int(os.environ.get("PORT", 8080)))

if __name__ == "__main__":
    my_app = WebServer()
    my_app.run()

我尝试使用

gunicorn --bind :$PORT --workers 1 --threads 8 --timeout 0 WebServer:my_app
来运行服务器,但是我得到了错误:

2023-04-10 01:41:27 Failed to find attribute 'my_app' in 'WebServer'.
2023-04-10 01:41:27 [2023-04-10 05:41:27 +0000] [6] [INFO] Worker exiting (pid: 6)
2023-04-10 01:41:27 [2023-04-10 05:41:27 +0000] [1] [INFO] Shutting down: Master
2023-04-10 01:41:27 [2023-04-10 05:41:27 +0000] [1] [INFO] Reason: App failed to load.

我可以要求正确的命令来运行这个包装在带有 gunicorn 的类中的 Flask 服务器吗?

谢谢!

python flask gunicorn google-cloud-run
© www.soinside.com 2019 - 2024. All rights reserved.