具有摄影机流的Flask应用程序中出现断管错误

2024-09-19 23:27:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我的代码中存在意外异常问题。我有两个来自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

我不知道怎么了。。。也许有人知道?将感谢您的帮助:)


Tags: nameinidstreamgetifmainvideo
1条回答
网友
1楼 · 发布于 2024-09-19 23:27:24

我假设您使用的是Flask开发服务器,它只是单线程的

发生的情况是,当您导航到另一个流时,浏览器会关闭连接,但这需要一些时间。因为只有一个线程,所以您的请求将等待初始流请求出错

所以,解决这个问题的一个方法就是通过使用类似uWSGI的东西来增加更多的工人

相关问题 更多 >