【问题标题】:BrokenPipeError in Flask app with camera streams带有相机流的 Flask 应用程序中的 BrokenPipeError
【发布时间】: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


    【解决方案1】:

    我假设您使用的是仅单线程的 Flask 开发服务器。

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

    因此,解决此问题的一种方法是通过使用 uWSGI 之类的东西来添加更多工作人员。

    【讨论】:

    • 另一种是在你的应用开始时添加threaded参数:app.run(host='127.0.0.1', threaded=True)
    • 我正在启动这样的应用程序:python3 -m flask run --host=ip_addr --port=port --with-threads 因为流和相机类都在不同的线程中
    • 好的,看起来这个默认行为在新版本的 Flask 中已经改变了,线程实际上是默认启用的。所以,问题一定是不同的东西,但是如果没有看到相机和流相关代码就不可能说出来
    • 当然 :) 但由于代码太长,无法发表评论,我将 liunk 发布到 git:gitlab.com/gawron103/peephole/-/tree/… 文件:Camera.py、DetectionVideoStream.py 和 NormalVideoStream.py
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-08
    • 1970-01-01
    • 1970-01-01
    • 2020-07-22
    • 2015-08-16
    • 2020-01-29
    • 1970-01-01
    相关资源
    最近更新 更多