我有一个用connexion构建的Flask服务。在脚本src/group/application/my_service/api/app.py
:中定义的函数create_app()
中初始化服务
# app.py
def create_app():
arguments = {"url": "0.0.0.0"}
app = connexion.App(__name__, options={"swagger_ui": True})
app.add_api("openapi-spec.yml", arguments=arguments, strict_validation=True)
app.run(port=8080, debug=True)
在src/group/application/my_service/__main__.py
中,我导入create_app
并执行它:
# __main__.py
from group.application.my_service.api.app import create_app
create_app()
有了这个,我就可以成功地用python打开服务:
python -m src.group.application.my_service
我现在想用gunicorn代替。我正在尝试以下命令
gunicorn -w 1 -b 0.0.0.0:8080 'src.group.application.my_service.api.app:create_app()'
但我收到以下错误消息:
[2021-05-19 11:55:32 +0200] [13275] [INFO] Starting gunicorn 20.1.0
[2021-05-19 11:55:32 +0200] [13275] [INFO] Listening at: http://0.0.0.0:8080 (13275)
[2021-05-19 11:55:32 +0200] [13275] [INFO] Using worker: sync
[2021-05-19 11:55:32 +0200] [13276] [INFO] Booting worker with pid: 13276
* Serving Flask app "src.group.application.my_service.api.app" (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: on
2021-05-19 11:55:33,672 [CRITICAL] Traceback (most recent call last):
File "/Users/user/repo_name/src/group/application/my_service/api/app.py", line 39, in create_app
app.run(port=8080, debug=True)
File "/Users/user/venvs/venv/lib/python3.9/site-packages/connexion/apps/flask_app.py", line 96, in run
self.app.run(self.host, port=self.port, debug=self.debug, **options)
File "/Users/user/venvs/venv/lib/python3.9/site-packages/flask/app.py", line 990, in run
run_simple(host, port, self, **options)
File "/Users/user/venvs/venv/lib/python3.9/site-packages/werkzeug/serving.py", line 1030, in run_simple
s.bind(server_address)
OSError: [Errno 48] Address already in use
Failed to find application object: 'create_app()'
[2021-05-19 11:55:33 +0200] [13276] [INFO] Worker exiting (pid: 13276)
[2021-05-19 11:55:33 +0200] [13275] [INFO] Shutting down: Master
[2021-05-19 11:55:33 +0200] [13275] [INFO] Reason: App failed to load.
我如何才能成功地使用Gunicorn打开服务,而不会收到关于我正在开发服务的警告信息(这是我想要使用Gunicorn的根本原因(?
原来函数creat_app()
应该返回app
,而不是调用app.run()