烧瓶/女服务员服务器因来自单个客户端的串行GET请求而超载

时间:2020-05-28 08:52:48

标签: python http flask waitress

我有一个与女服务员服务器一起部署的Flask应用程序。始终只有一个客户端浏览器访问服务器(这是一个内部项目)。大型图像数据库应该在浏览器中一次流化一个图像(图像是从服务器上下载的,并通过db进行迭代)。

服务器端

进行一些设置后,该应用会流式传输图像数据库 URL some_ip_address/get_next_image。收到get_next_image上的每个请求后,服务器 获取下一张图像,对其进行预处理,然后将文件作为响应发送。服务器启动使用 waitress.serve(app, host='0.0.0.0', port=5000)

客户端

重定向到some_ip_address/stream后,HTML文件stream.html在浏览器中显示。 该页面立即调用streamNextImage(),并在get_next_image上发出GET请求。一旦 下载图像,将其设置为在src页面上呈现的图像的stream属性,然后 还设置了超时以请求新图像(约0.2秒)。

问题

服务器按预期工作一段时间(有时需要一个小时,有时需要更长的时间),但最终会导致响应时间 开始变得不稳定(卡住10秒钟,然后再次正常响应),之后响应之间的间隔增加 到10秒的时间,最终服务器完全停止发送图像。

发生这种情况时,即使其他URL在浏览器中也没有响应,但是在服务器日志中我收到一条消息 [WARNING] Task queue depth is 1,尽管服务器未发送任何内容(或接收请求),但计算机上的CPU使用率 它的运行位置很高,但我不知道它在做什么。

好像服务器超载了先前的请求,但我不明白为什么,因为客户端JS的设置方式 仅在收到当前图像后才发出新请求,因此应该不会造成压倒性的麻烦。

我该如何调试呢?或者可能是问题所在?

服务器代码

@app.route("/get_next_image")
def get_next_image():
    row_idx, row, _ = next(app.config["db_iterator"])
    image = preprocess_image(row_idx, row)
    cv2.imwrite('stream_image.jpg', image)
    return send_file(image_path)

客户端浏览器代码

# stream.html
<html>
<meta http-equiv='cache-control' content='no-cache'>
<meta http-equiv='expires' content='0'>
<meta http-equiv='pragma' content='no-cache'>
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script>
        function streamNextImage() {
            // Change images only when the new one is already downloaded
            nextImgSrc = '{{ url_for('get_next_image') }}?' + new Date().getTime(); // Add current time as param to prevent browser caching of this URL
            tmpImg.src = nextImgSrc;
            tmpImg.onload = function () {
                $('#face_image').attr('src', nextImgSrc);
                setTimeout(streamNextImage, 1000 * "{{ wait_period }}");
            }
        }

        var tmpImg = new Image();
        streamNextImage()
    </script>
</head>
<body>
    <img src='' alt="Image" id="face_image" name="face_image">
</body>
</html>

服务器停止响应时最终服务器日志输出

2020-05-28 05:05:16 [INFO ]  Streaming idx:6134/53122)
2020-05-28 05:05:17 [INFO ]  <Request 'http://localhost:5000/get_next_image?1590642317559' [GET]>
2020-05-28 05:05:17 [INFO ]  Streaming idx:6135/53122)
2020-05-28 05:05:26 [INFO ]  <Request 'http://localhost:5000/get_next_image?1590642326559' [GET]>
2020-05-28 05:05:26 [INFO ]  Streaming idx:6136/53122)
2020-05-28 05:05:27 [INFO ]  <Request 'http://localhost:5000/get_next_image?1590642327559' [GET]>
2020-05-28 05:05:27 [INFO ]  Streaming idx:6137/53122)
2020-05-28 05:05:35 [INFO ]  <Request 'http://localhost:5000/get_next_image?1590642332559' [GET]>
2020-05-28 05:05:35 [INFO ]  Streaming idx:6138/53122)
2020-05-28 05:05:36 [INFO ]  <Request 'http://localhost:5000/get_next_image?1590642336559' [GET]>
2020-05-28 05:05:36 [INFO ]  Streaming idx:6139/53122)
2020-05-28 08:05:22 [WARNI]  Task queue depth is 1

0 个答案:

没有答案