Pi Flask Video streaming stops error: [Errno 32] Broken pipe

Pi Flask Video streaming stops error: [Errno 32] Broken pipe

我有 Pi B2 模型,其中我有 Pi Cam 模块,我有 python flask 应用程序,我从 以下 miguelgrinberg 博客 http://blog.miguelgrinberg.com/post/video-streaming-with-flask

https://github.com/miguelgrinberg/flask-video-streaming

并且我添加了 gevent 作为网络服务器来服务多线程相机流连接和 修改脚本如下

#!/usr/bin/env python
from flask import Flask, render_template, Response
from gevent import monkey; monkey.patch_all()


# Raspberry Pi camera module (requires picamera package)
from camera_pi import Camera

app = Flask(__name__)


@app.route('/')
def index():
    """Video streaming home page."""
    return render_template('index.html')


def gen(camera):
    """Video streaming generator function."""
    while True:
        frame = camera.get_frame()
        yield (b'--frame\r\n'
               b'Content-Type: image/jpeg\r\n\r\n' + frame + 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(Camera()),
                    mimetype='multipart/x-mixed-replace; boundary=frame')


if __name__ == '__main__':
    monkey.patch_all()
    from gevent.wsgi import WSGIServer
    WSGIServer(('', 5000),app).serve_forever()
    #app.run(host='0.0.0.0', debug=True, threaded=True)

每次我启动服务器并尝试使用客户端访问它时,我都会收到此错误消息

随着该服务器在启动后大约 30 分钟或更长时间后停止流式传输 mjpeg。我还注释掉了没有连接的部分 接下来的 10 秒

In Camera_pi.py file

# if there hasn't been any clients asking for frames in**
            # the last 10 seconds stop the thread
            #if time.time() - cls.last_access > 10000:
            #    break

客户端连接到服务器后的错误消息,即使出现此消息我仍然可以查看流媒体,但在浏览器中的 30 分钟以上帧冻结后不超过 30 分钟或更长时间,刷新也不起作用我必须按 ctrl+c python app.py 然后重新开始:

(streampi)pi@niravpi ~/svsapp/streampi $ python app.py
Traceback (most recent call last):
  File "/home/pi/svsapp/streampi/local/lib/python2.7/site-packages/gevent/pywsgi.py", line 508, in handle_one_response
    self.run_application()
  File "/home/pi/svsapp/streampi/local/lib/python2.7/site-packages/gevent/pywsgi.py", line 495, in run_application
    self.process_result()
  File "/home/pi/svsapp/streampi/local/lib/python2.7/site-packages/gevent/pywsgi.py", line 486, in process_result
    self.write(data)
  File "/home/pi/svsapp/streampi/local/lib/python2.7/site-packages/gevent/pywsgi.py", line 376, in write
    self._write(data)
  File "/home/pi/svsapp/streampi/local/lib/python2.7/site-packages/gevent/pywsgi.py", line 369, in _write
    self._sendall(data)
  File "/home/pi/svsapp/streampi/local/lib/python2.7/site-packages/gevent/pywsgi.py", line 355, in _sendall
    self.socket.sendall(data)
  File "/home/pi/svsapp/streampi/local/lib/python2.7/site-packages/gevent/socket.py", line 460, in sendall
    data_sent += self.send(_get_memory(data, data_sent), flags)
  File "/home/pi/svsapp/streampi/local/lib/python2.7/site-packages/gevent/socket.py", line 445, in send
    return sock.send(data, flags)
error: [Errno 32] Broken pipe
{'GATEWAY_INTERFACE': 'CGI/1.1',
 'HTTP_ACCEPT': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
 'HTTP_ACCEPT_ENCODING': 'gzip, deflate, sdch',
 'HTTP_ACCEPT_LANGUAGE': 'en-US,en;q=0.8,hi;q=0.6',
 'HTTP_CONNECTION': 'keep-alive',
 'HTTP_HOST': '192.168.1.6:5000',
 'HTTP_UPGRADE_INSECURE_REQUESTS': '1',
 'HTTP_USER_AGENT': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.80 Safari/537.36',
 'PATH_INFO': '/video_feed',
 'QUERY_STRING': '',
 'REMOTE_ADDR': '192.168.1.4',
 'REMOTE_PORT': '55311',
 'REQUEST_METHOD': 'GET',
 'SCRIPT_NAME': '',
 'SERVER_NAME': 'niravpi',
 'SERVER_PORT': '5000',
 'SERVER_PROTOCOL': 'HTTP/1.1',
 'SERVER_SOFTWARE': 'gevent/1.0 Python/2.7',
 'werkzeug.request': None,
 'wsgi.errors': <open file '<stderr>', mode 'w' at 0x76d850d0>,
 'wsgi.input': <gevent.pywsgi.Input object at 0x763cb5d0>,
 'wsgi.multiprocess': False,
 'wsgi.multithread': False,
 'wsgi.run_once': False,
 'wsgi.url_scheme': 'http',
 'wsgi.version': (1, 0)} failed with error

192.168.1.4 - - [2015-10-31 14:44:53] "GET /video_feed HTTP/1.1" socket 479452 5.199595

管道破裂错误表明 Flask 试图写入从另一端关闭的套接字,即客户端。

如果您正在进行流式传输并突然关闭浏览器,您将收到此错误 window。在这种情况下,错误是无害的,它只会导致为该客户端提供服务的线程停止,这正是客户端消失时您想要的。

您也会在任何类型的连接中断时收到此错误。为了使流式传输更加稳健,您需要放置一个系统来检查客户端连接是否处于活动状态,当它不活动时,可能会触发图像的重新加载,以便重新建立流式传输。

要检查客户端是否保持连接,您可以记录客户端获取最后一个视频帧时的时间戳。然后客户端发送的 Ajax 调用可以检查检索帧的时间,如果这比某个阈值长,则声明连接断开并触发客户端的图像刷新。