[英]gunicorn + flask (connexion/swagger_server) time out/not responding to API requests
swagger_server connexion/flask running fine when I do:当我这样做时,swagger_server 连接/烧瓶运行良好:
python3 -m swagger_server
It's running on port 8080.它在端口 8080 上运行。
When I try to put it on gunicorn (reference: how to use gunicorn with swagger_server on flask ), gunicorn runs fine but requests to port 8080 fail.当我尝试将它放在 gunicorn 上时(参考: 如何在 flask 上使用带有 swagger_server 的 gunicorn ),gunicorn 运行良好但对端口 8080 的请求失败。
First, when I use the same port 8080, it complains of bind/already in use (expected, I believe, as they're both on port 8080):首先,当我使用相同的端口 8080 时,它会抱怨绑定/已经在使用中(我相信,因为它们都在端口 8080 上):
gunicorn "swagger_server.__main__:main" -b 0.0.0.0:8080 -w 4
...
OSError: [Errno 48] Address already in use
But when I move to port 4000, for example, requests time out:但是当我移动到端口 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)
When I enable debug, I see it's trying to connect to port 4000, instead of 8080.当我启用调试时,我看到它正在尝试连接到端口 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)
...
Here's my main.py这是我的 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)
Please advise what I'm missing here.请告知我在这里缺少什么。 Thank you.谢谢你。
Swagger generates __main__.py
in wrong way, you need to make a modifition base on it. 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)
Then try it again然后再试一次
gunicorn swagger_server.__main__:app
Gunicorn wraps netowrk request in WSGI and forward to flask(werkzeug), the varible app
in __main__.py
is werkzeug WSGI entry point. Gunicorn包装在WSGI和着烧瓶(WERKZEUG)元网络请求,varible app
在__main__.py
是WERKZEUG WSGI入口点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.