【发布时间】:2020-11-14 09:10:57
【问题描述】:
我的代码中有意外异常的问题。我有两个来自 RPi 相机的视频流,我通过按钮在它们之间切换(现在我正在台式计算机上测试所有内容)。代码如下所示:
用户个人资料:
@main.route('/profile', methods=["POST", "GET"])
def profile():
if not current_user.is_authenticated:
abort(403)
return render_template('profile.html', name=current_user.name, id=current_user.id, detectionState=current_user.detectionState)
视频流:
@main.route('/video_stream/<int:stream_id>')
def video_stream(stream_id):
if not current_user.is_authenticated:
abort(403)
print(f'Current user detection: {current_user.detectionState}')
global detectionStream
global normalStream
stream = None
if current_user.detectionState:
stream = detectionStream
print('Stream set to detection one')
else:
stream = normalStream
print('Stream set to normal one')
return Response(stream.gen(), mimetype='multipart/x-mixed-replace; boundary=frame')
换流:
@main.route('/detection')
def detection():
if not current_user.is_authenticated:
abort(403)
if current_user.detectionState:
current_user.detectionState = False
else:
current_user.detectionState = True
user = User.query.filter_by(id=current_user.id)
user.detectionState = current_user.detectionState
db.session.commit()
return redirect(url_for('main.profile', id=current_user.id, user_name=current_user.name))
一切正常。我可以看到两个流并在它们之间切换。但我也看到后台有一些例外情况会减慢整个应用程序的速度。这是发生了什么:
127.0.0.1 - - [24/Jul/2020 15:19:59] "GET /profile?id=2&user_name=piotrek HTTP/1.1" 200 -
Current user detection: True
Stream set to detection one
127.0.0.1 - - [24/Jul/2020 15:19:59] "GET /video_stream/2 HTTP/1.1" 200 -
127.0.0.1 - - [24/Jul/2020 15:20:01] "GET /detection HTTP/1.1" 302 -
127.0.0.1 - - [24/Jul/2020 15:20:01] "GET /profile?id=2&user_name=piotrek HTTP/1.1" 200 -
Traceback (most recent call last):
File "/Users/pio/Library/Python/3.7/lib/python/site-packages/werkzeug/serving.py", line 295, in execute
write(data)
File "/Users/pio/Library/Python/3.7/lib/python/site-packages/werkzeug/serving.py", line 276, in write
self.wfile.write(data)
File "/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.7/lib/python3.7/socketserver.py", line 799, in write
self._sock.sendall(b)
BrokenPipeError: [Errno 32] Broken pipe
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/pio/Documents/programowanie/git_projects/Peephole/server/DetectionVideoStream.py", line 28, in gen
b'Content-Type: image/jpeg\r\n\r\n' + self.__img + b'\r\n')
GeneratorExit
Detection video stream genenation exception
Exception ignored in: <generator object DetectionVideoStream.gen at 0x11c84c4f8>
RuntimeError: generator ignored GeneratorExit
Current user detection: False
Stream set to normal one
我不知道出了什么问题...也许有人知道?将不胜感激帮助:)
【问题讨论】:
标签: python python-3.x sockets opencv flask