Gevent, приложение Flask зависало во время потокового видео, пока клиент не отключился - PullRequest
0 голосов
/ 27 апреля 2018

Я использую gevent и flask для создания потокового приложения. Я встроил элемент img в html-страницу и установил 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

Может кто-нибудь посоветовать?

...