【问题标题】:Flask App can't handle more than 6 requests with a single browserFlask App 不能用单个浏览器处理超过 6 个请求
【发布时间】:2019-11-26 11:47:19
【问题描述】:

我正在尝试使用 Flask、OpenCV 制作一个通过 Chrome 浏览器显示多个视频处理流式传输结果的摄像头服务器。

问题

单个浏览器最多可以处理6个请求,但我需要处理60多个摄像头请求。

我尝试过的事情。

起初我认为 CPU 不能处理超过 6 个输入,但是当我从另一个浏览器(chrome、Firefox、Edge 等)请求它时,它可以同时处理更多。所以硬件似乎不是问题。它以某种方式限制了每个浏览器的请求。所以每个浏览器最多只能处理 6 个。

如果我请求超过 6 个,它会开始处理前 6 个请求并等待其中一个请求完成,然后再开始下一个请求。我认为这是因为 Flask 应用程序同步处理进程。

我已经尝试使用 Flask 的 thread=True 选项,以及用于异步进程的 gunicorn。但没有区别。

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

我不知道为什么它仅限于 6 个请求。 有办法解决吗?

这是该项目的示例代码。

video.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)

【问题讨论】:

  • 只是一个想法,但是如何使用一些更专注于视频流的软件,如 VLC?
  • 您是否尝试过指定processes=xx 而不是threaded=True?即使它无法正常工作,您也可能会从中获得有用的日志。
  • 您是否尝试运行应用程序,而不是在调试模式下使用烧瓶服务器?例如 gunicorn 和多个工人。
  • @KlausD.我不能,因为我必须将它用于我的 Web 客户端服务器
  • @raphael_mav 你是说这个吗? app.run(debug=False, host='0.0.0.0', port=3999, threaded=False, processes=20) 对这个不起作用

标签: python google-chrome opencv flask gunicorn


【解决方案1】:

它的浏览器的 TCP 连接仅限于 6 个。

reference

如何解决这个问题

Firefox 可以从 about:config 中配置,在 network.http 上过滤各种设置; network.http.max-persistent-connections-per-server 是要更改的。

ref

但由于是浏览器的问题,我可能会使用不同的方法,例如创建具有不同端口的同一服务器。

【讨论】:

  • 谢谢!!花了一天时间调试这个,试图弄清楚为什么烧瓶/guncorn/gevent 不会占用超过 6 个连接!对于 chrome 作为一种解决方法,我们在一个页面上有多个烧瓶服务器和 iframe 以显示超过 6 个视频流。
【解决方案2】:

我建议使用 uwsgi 并与工作人员/流程一起扩展。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-11-24
    • 1970-01-01
    • 2018-08-10
    • 1970-01-01
    • 2011-04-07
    • 2020-04-14
    • 2013-01-18
    • 1970-01-01
    相关资源
    最近更新 更多