我正在使用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'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame.tobytes() + b'\r\n')
@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')
问题:流式传输工作正常,但问题是Flask阻塞并拒绝接受新请求,直到客户端断开连接!
这是我用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
有人可以建议吗?