配置 uwsgi 超时

Configure uwsgi timeout

我努力为 uwsgi 配置响应超时,文档没有帮助。我在客户端有很长的硬超时查询,所以我想在连接到 Flask 之前断开连接,或者有一些方法来检查请求是否在监听队列中超过某个阈值,因为不需要回答将被故意丢弃的请求.我也有 CPU 的限制,所以我想正好有 X 名工人同时工作。在当前的 Locust 设置中,我在短时间后获得了 100% 的失败率。

这是烧瓶应用程序

from flask import Flask, request
import requests
import json
import time
import uwsgi

app = Flask(__name__)

@app.route('/')
def test_request():
    # This is always false
    #if not uwsgi.is_connected(uwsgi.connection_fd()):
    #    print("Disconnected")

    time.sleep(4) # Long query
    return "Done"

if __name__ == "__main__":
    app.run(host='0.0.0.0')

uwsgi 配置 运行 与 uwsgi uwsgi.ini

[uwsgi]
strict = true
module = flask:app
master = true
processes = 2
threads = 1
enable-threads = true
listen = 8
# I don't want to kill workers
#harakiri = 60 
http-timeout = 5
need-app = true
lazy = true
# Not clear what the difference with http-timeout
http-connect-timeout = 5

http = 0.0.0.0:5000

蝗虫配置

from locust import HttpUser, between, task, constant


class MyWebsiteUser(HttpUser):
    wait_time = constant(1)

    @task
    def load_main(self):
        self.client.get("/", timeout=10) # Hard client timeout

我将结果解释为 uwsgi 要么不遵守超时,要么此配置与 request/response 超时无关,但仍在尝试响应掉线的客户端。

我不限于 uwsgi 但它似乎是最先进的。

我找到的最接近的解决方案来自 AWS Sagemaker example

有效的解决方案是使用 nginx 作为速率限制器,使用 gunicorn 作为负载均衡器。在我的蝗虫测试中,为了避免队列溢出,我不得不添加 limit_req_zone 和静态键。

系统吞吐量增加了三个数量级,Flask 本身就是不可扩展性的典范。