简体   繁体   English

gunicorn + flask (connexion/swagger_server) 超时/不响应 API 请求

[英]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.

相关问题 如何在烧瓶上使用带有 swagger_server 的 gunicorn - how to use gunicorn with swagger_server on flask 没有名为 swagger_server 的模块 - No module named swagger_server 如何指定用于使用 Flask-connexion 和 swagger-2.0 API 验证基本身份验证的函数 - How to specify the function used to validate the Basic auth with Flask-connexion and swagger-2.0 API 使用gunicorn作为wsgi服务器将请求记录到Flask服务器上……到AWS CloudWatch - Log requests to a flask server using gunicorn as wsgi server … to AWS cloudwatch gunicorn 无法启动 flask 服务器:ModuleNotFoundError:没有名为“请求”的模块 - gunicorn fails to launch flask server: ModuleNotFoundError: No module named 'requests' 速率限制 REST API 与连接和 swagger - Rate limit REST API made with connexion and swagger 第一个使用connexion + Flask + Swagger的python微服务出错 - Error in my first python microservices with connexion+Flask+Swagger 使用Flask和eventlet响应并发请求 - Responding to concurrent requests with Flask and eventlet 使用龙卷风在Connexion(Swagger Codegen)Python服务器中异步 - Async in connexion (swagger codegen) python server using tornado 使用Gunicorn和Flask查看传入请求登录控制台 - View incoming requests log in console with Gunicorn and Flask
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM