Pi Flask视频流停止错误:[Errno 32]管道损坏

时间:2015-10-31 15:41:30

标签: python flask raspberry-pi video-streaming gevent

我有Pi B2模型,其中我有Pi Cam模块,我有python flask应用程序,我已经跟随 关注miguelgrinberg博客 http://blog.miguelgrinberg.com/post/video-streaming-with-flask

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

我添加gevent作为Web服务器来提供多线程相机流连接和 修改后的脚本如下

#!/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秒

  

在Camera_pi.py文件中

# 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 + min帧后刷新也无法正常工作我必须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

1 个答案:

答案 0 :(得分:0)

损坏的管道错误表明Flask试图写入从另一端关闭的套接字,这是客户端的一面。

如果您正在进行流式传输并突然关闭浏览器窗口,则会出现此错误。在这种情况下,错误是无害的,它只会导致服务该客户端的线程停止,这是客户端离开时所需的。

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

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