[英]Flask server with threading gives “error: [Errno 32] Broken pipe”
[英]Pi Flask Video streaming stops error: [Errno 32] Broken pipe
我有Pi B2模型,其中有Pi Cam模块和python烧瓶应用程序,我从以下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分钟以上不能超过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试图写入从另一端(客户端一侧)关闭的套接字。
如果您正在进行流式传输并突然关闭浏览器窗口,则会收到此错误。 在这种情况下,该错误是无害的,它只会导致正在为该客户端提供服务的线程停止,这是客户端离开时想要的。
您还会在任何类型的连接中断中收到此错误。 为了使流传输更加健壮,您需要将一个系统安装到位,以检查客户端连接是否处于活动状态,如果连接不活跃,则可能会触发映像重新加载,以便重新建立流传输。
要检查客户端是否保持连接状态,可以记录客户端获取最后一个视频帧时的时间戳。 然后,客户端发送的Ajax调用可以检查检索帧的时间,如果该帧长于某个阈值,则可以声明连接断开并触发来自客户端的图像刷新。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.