[英]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.