简体   繁体   English

Flask App 不能用一个浏览器处理超过 6 个请求

[英]Flask App can't handle more than 6 requests with a single browser

I'm trying to make a camera server that shows multiple video process streaming results through Chrome browser using Flask, OpenCV.我正在尝试使用 Flask、OpenCV 制作一个通过 Chrome 浏览器显示多个视频处理流结果的相机服务器。

Problem问题

It can handle 6 request at most with a single browser, but I need to process more than 60 camera requests.单个浏览器最多可以处理6个请求,但是我需要处理60多个摄像头请求。

Things I've tried.我尝试过的事情。

At first I thought CPU couldn't handle more than 6 inputs but when I request it from another browser(chrome,Firefox,Edge etc), it can handle more simultaneously.起初我认为 CPU 不能处理超过 6 个输入,但是当我从另一个浏览器(chrome、Firefox、Edge 等)请求它时,它可以同时处理更多。 So hardware seems not to be a problem.所以硬件似乎不是问题。 It's somehow limiting the requests per browser.它以某种方式限制了每个浏览器的请求。 So every single browser could handle only 6 at most.所以每个浏览器最多只能处理 6 个。

If I request more than 6 it starts processing the first 6 request and wait till one of them finishes then starts next requests.如果我请求超过 6 个,它开始处理前 6 个请求并等待其中一个完成,然后开始下一个请求。 I think it's because Flask app handles process synchronously.我认为这是因为 Flask 应用程序同步处理进程。

I've tried with thread=True options with Flask, and with gunicorn for async process.我已经尝试过使用 Flask 的thread=True选项,以及用于异步进程的 gunicorn。 but there is no difference.但没有区别。

gunicorn video:app -w 81 --threads 81 -k gevent --worker-connections 1000

I have no idea why it's limited to 6 requests only.我不知道为什么它仅限于 6 个请求。 is it possible to solve this?有可能解决这个问题吗?

Here is a sample code for the project.这是该项目的示例代码。

video.py视频.py

import cv2
from flask import Flask,request, Response


def loadVideo(video):
    cap = cv2.VideoCapture(video)

    if cap.isOpened(): # try to get the first frame
        rval, frame = cap.read()
    else:
        rval = False


    IMG_SIZE = 640
    while(1):
        rval, image = cap.read()
        image = cv2.resize(image, (IMG_SIZE, IMG_SIZE))  

        if rval==True:
            orig = image.copy()
            frame = cv2.blur(image, (3,3))

            yield (b'--frame\r\n' b'Content-Type: image/jpeg\r\n\r\n'+ cv2.imencode('.jpg', frame)[1].tostring() + b'\r\n')

        elif rval==False:
                break
    end = time.time()
    cap.release()

app = Flask(__name__)

@app.route('/')
def hello():
    return "Server Established!"

@app.route('/detect',methods = ['GET'])
def detect():
    try:
        if request.method == 'GET':
            src = request.args.get('src')
            return Response(loadVideo(src),
        mimetype = "multipart/x-mixed-replace; boundary=frame")
    except Exception as e:
        return e


if __name__ == '__main__':
    app.run(debug=True, host='0.0.0.0', port=3999, threaded=True)

It's browser's TCP connection is limited to 6 only.它的浏览器的 TCP 连接仅限于 6 个。

reference 参考

How to solve this如何解决这个问题

Firefox can be configured from within about:config, filter on network.http for various settings; Firefox 可以在 about:config 中配置,在 network.http 上过滤各种设置; network.http.max-persistent-connections-per-server is the one to change. network.http.max-persistent-connections-per-server是要改变的。

ref 参考

But as it's browser's problem I may use different approach like creating same server with different port.但由于这是浏览器的问题,我可能会使用不同的方法,例如使用不同的端口创建相同的服务器。

我建议使用uwsgi并根据工作人员/流程进行扩展。

uwsgi --http :9090 --wsgi-file foobar.py --master --processes 4 --threads 2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM