gevent,烧瓶应用程序在流式传输视频时陷入困境,直到客户断开连接为止



我正在使用gevent和flask创建流媒体应用程序。我嵌入了HTML页面中的img元素,并设置其src=/video_feed

相应的烧瓶代码是

def gen():
    global vc
    """Video streaming generator function."""
    if vc is None:
        vc = cv2.VideoCapture(0)
        _, frame = vc.read()
    while True:        
        rval, frame = vc.read()
        r, frame = cv2.imencode('.jpg', frame)
        yield (b'--framern'
               b'Content-Type: image/jpegrnrn' + frame.tobytes() + b'rn')
@app.route('/video_feed')
def video_feed():
    """Video streaming route. Put this in the src attribute of an img tag."""
    return Response(gen(),
                    mimetype='multipart/x-mixed-replace; boundary=frame')

问题:流媒体效果很好,但问题是烧瓶块,拒绝接受新请求,直到客户端断开连接为止!

这是我用gevent

启动它的代码
def start_gevent(app_port):
    http_server = WSGIServer(('', app_port), app)
    http_server.serve_forever()

这是我的应用记录。

::1 - - [2018-04-27 18:02:56] "GET / HTTP/1.1" 200 1827 0.008464
::1 - - [2018-04-27 18:02:59] "GET /video.html HTTP/1.1" 200 1850 <--streaming starts here
 0.001877 [wxpython.py] OnClose called <-- client disconnect
::1 - - [2018-04-27 18:03:01] "GET /video_feed HTTP/1.1" 200 2951956 2.790426 <-- flask log of the request comes late
::1 - - [2018-04-27 18:03:01] "GET /database.html HTTP/1.1" 200 118 0.003394 <-- client made these requests concurrently, but flask didnt respond, it logs them now
::1 - - [2018-04-27 18:03:01] "GET /database.html HTTP/1.1" 200 118 0.000549

任何人都可以建议吗?

只需在发电机中添加一个time.sleep(100/1000)即可起作用

最新更新