首页 > 解决方案 > Gunicorn 内存配额超出

问题描述

我有一个需要在浏览器中显示视频流的 python 应用程序。为了显示流,我向另一个路由(/video_feed)发出请求。这是代码:

@app.route("/video_feed")
def video_feed():
    # return the response generated along with the specific media
    # type (mime type)
    return Response(generate(),
                    mimetype="multipart/x-mixed-replace; boundary=frame")
def generate():
    # grab global references to the output frame and lock variables
    global outputFrame, lock

    # loop over frames from the output stream
    while True:
        # wait until the lock is acquired
        with lock:
            # check if the output frame is available, otherwise skip
            # the iteration of the loop
            if outputFrame is None:
                continue

            # encode the frame in JPEG format
            (flag, encodedImage) = cv2.imencode(".jpg", outputFrame)

            # ensure the frame was successfully encoded
            if not flag:
                continue

        # yield the output frame in the byte format
        yield (b'--frame\r\n' b'Content-Type: image/jpeg\r\n\r\n' +
               bytearray(encodedImage) + b'\r\n')
<html>
  <head>
    <title>Pi Video Surveillance</title>
  </head>
  <body>
    <h1>Pi Video Surveillance</h1>
    <img src="{{ url_for('video_feed') }}">
  </body>
</html>
@app.route('/stream')
def show_video_stream():
    return render_template("stream.html")
def prepare_video_stream():
    print("It might be streaing")
    global outputFrame, lock
    coordinates_file = "data/parking_spot_coordinates.yml"
    with open(coordinates_file, "r") as coordinates_data:
        coordinates = yaml.load(coordinates_data)
        stream_url = os.environ.get('RTSP_URL')
        video_stream = VideoStream(coordinates)
        # video_stream.stream_video()
        video_capture = cv2.VideoCapture(stream_url)
        fps = video_capture.get(cv2.CAP_PROP_FPS)
        print(fps)
        video_capture.set(cv2.CAP_PROP_BUFFERSIZE, 0)

        while video_capture.isOpened():
            frame = video_stream.get_frame(video_capture)
            # acquire the lock, set the output frame, and release the
            # lock
            with lock:
                outputFrame = frame.copy()
@app.before_first_request
def start_stream_processing_thread():
    # # start a thread that will perform motion detection
    t = threading.Thread(target=prepare_video_stream)
    t.daemon = True
    t.start()

使用内置的烧瓶服务器在本地运行时一切运行良好,但我尝试将应用程序部署到 heroku,并使用 gunicorn 作为服务器。这是Procfile:

web: gunicorn app:app

检查heroku日志时,我得到以下信息:错误R14(超出内存配额)

我怎样才能避免这种情况?

标签: pythonmultithreadingflaskherokugunicorn

解决方案


推荐阅读