gunicorn + flask (connexion/swagger_server) 时间 out/not 响应 API 请求
gunicorn + flask (connexion/swagger_server) time out/not responding to API requests
swagger_server connexion/flask 运行 当我做的时候很好:
python3 -m swagger_server
在端口 8080 上 运行。
当我尝试将它放在 gunicorn 上时(参考:),gunicorn 运行良好但对端口 8080 的请求失败。
首先,当我使用同一个端口 8080 时,它会抱怨正在使用 bind/already(我相信这是预期的,因为它们都在端口 8080 上):
gunicorn "swagger_server.__main__:main" -b 0.0.0.0:8080 -w 4
...
OSError: [Errno 48] Address already in use
但是当我移动到端口 4000 时,例如,请求超时:
gunicorn "swagger_server.__main__:main" -b 0.0.0.0:4000 -w 4
...
* Running on http://0.0.0.0:8080/ (Press CTRL+C to quit)
[2020-02-16 17:21:18 -0500] [34798] [CRITICAL] WORKER TIMEOUT (pid:34802)
当我启用调试时,我看到它正在尝试连接到端口 4000,而不是 8080。
[2020-02-16 17:28:13 -0500] [34866] [INFO] Starting gunicorn 20.0.4
[2020-02-16 17:28:13 -0500] [34866] [ERROR] Connection in use: ('0.0.0.0', 4000)
[2020-02-16 17:28:13 -0500] [34866] [ERROR] Retrying in 1 second.
[2020-02-16 17:28:14 -0500] [34866] [ERROR] Connection in use: ('0.0.0.0', 4000)
...
这是我的 main.py
def main(arg1, arg2):
app = connexion.App(__name__, specification_dir='./swagger_server/', debug=False)
app.app.json_encoder = encoder.JSONEncoder
app.add_api('api-v2.yaml', arguments={'title': 'API'})
app.run(host='0.0.0.0', port=8080)
if __name__ == '__main__':
main(None, None)
请告知我在这里缺少什么。谢谢。
Swagger 生成__main__.py
的方式不对,需要修改
#!/usr/bin/env python3
import connexion
from swagger_server import encoder
app = connexion.App(__name__, specification_dir='./swagger/')
app.app.json_encoder = encoder.JSONEncoder
app.add_api('swagger.yaml', arguments={'title': 'My API'}, pythonic_params=True)
然后再试一次
gunicorn swagger_server.__main__:app
Gunicorn 将网络请求包装在 WSGI 中并转发给 flask(werkzeug),__main__.py
中的变量 app
是 werkzeug WSGI 入口点。
swagger_server connexion/flask 运行 当我做的时候很好:
python3 -m swagger_server
在端口 8080 上 运行。
当我尝试将它放在 gunicorn 上时(参考:
首先,当我使用同一个端口 8080 时,它会抱怨正在使用 bind/already(我相信这是预期的,因为它们都在端口 8080 上):
gunicorn "swagger_server.__main__:main" -b 0.0.0.0:8080 -w 4
...
OSError: [Errno 48] Address already in use
但是当我移动到端口 4000 时,例如,请求超时:
gunicorn "swagger_server.__main__:main" -b 0.0.0.0:4000 -w 4
...
* Running on http://0.0.0.0:8080/ (Press CTRL+C to quit)
[2020-02-16 17:21:18 -0500] [34798] [CRITICAL] WORKER TIMEOUT (pid:34802)
当我启用调试时,我看到它正在尝试连接到端口 4000,而不是 8080。
[2020-02-16 17:28:13 -0500] [34866] [INFO] Starting gunicorn 20.0.4
[2020-02-16 17:28:13 -0500] [34866] [ERROR] Connection in use: ('0.0.0.0', 4000)
[2020-02-16 17:28:13 -0500] [34866] [ERROR] Retrying in 1 second.
[2020-02-16 17:28:14 -0500] [34866] [ERROR] Connection in use: ('0.0.0.0', 4000)
...
这是我的 main.py
def main(arg1, arg2):
app = connexion.App(__name__, specification_dir='./swagger_server/', debug=False)
app.app.json_encoder = encoder.JSONEncoder
app.add_api('api-v2.yaml', arguments={'title': 'API'})
app.run(host='0.0.0.0', port=8080)
if __name__ == '__main__':
main(None, None)
请告知我在这里缺少什么。谢谢。
Swagger 生成__main__.py
的方式不对,需要修改
#!/usr/bin/env python3
import connexion
from swagger_server import encoder
app = connexion.App(__name__, specification_dir='./swagger/')
app.app.json_encoder = encoder.JSONEncoder
app.add_api('swagger.yaml', arguments={'title': 'My API'}, pythonic_params=True)
然后再试一次
gunicorn swagger_server.__main__:app
Gunicorn 将网络请求包装在 WSGI 中并转发给 flask(werkzeug),__main__.py
中的变量 app
是 werkzeug WSGI 入口点。