I am trying to create a live streaming camera with my Raspberry Pi and the Pi Camera module. I have chosen to create it with Flask so that I can use the Python API for it. Though when I run this script:

from flask import *
from picamera import PiCamera

app = Flask(__name__)

camera = PiCamera()

def index():
    return render_template("index.html")

def getImage():
     return send_file("./templates/image.jpg")

def getTheScript():
     return render_template("script.js")

if __name__ == "__main__":
     app.run(debug=True, host="")

I get the following error:

mmal: mmal_vc_port_enable: failed to enable port vc.null_sink:in:0(OPQV): ENOSPC
mmal: mmal_port_enable: failed to enable connected port (vc.null_sink:in:0(OPQV))0x131e7b0 (ENOSPC)
mmal: mmal_connection_enable: output port couldn't be enabled
Traceback (most recent call last):
  File "/home/pi/Desktop/camera_flask/app.py", line 6, in <module>
    camera = PiCamera()
  File "/usr/lib/python3/dist-packages/picamera/camera.py", line 417, in __init__
  File "/usr/lib/python3/dist-packages/picamera/camera.py", line 496, in _init_preview
    self, self._camera.outputs[self.CAMERA_PREVIEW_PORT])
  File "/usr/lib/python3/dist-packages/picamera/renderers.py", line 512, in __init__
  File "/usr/lib/python3/dist-packages/picamera/mmalobj.py", line 1465, in connect
    self._connection = MMALConnection(source, self.inputs[0])
  File "/usr/lib/python3/dist-packages/picamera/mmalobj.py", line 1278, in __init__
    prefix="Failed to enable connection")
  File "/usr/lib/python3/dist-packages/picamera/exc.py", line 157, in mmal_check
    raise PiCameraMMALError(status, prefix)
picamera.exc.PiCameraMMALError: Failed to enable connection: Out of resources (other than memory)

My first assumption based on the Failed to enable connection: Out of resources (other than memory) part is that the pi was was not receiving enough power. So I got a 5V - 2.5A micro USB power adapter instead of the 5.1V - 1A adapter that I was using and it still shows this error.

Hope you can help

~ Iwotastic

  • Try gooling for "picamera mmal out of resources".
    – jogco
    Commented Jul 18, 2016 at 21:47
  • Looks very similar to: raspberrypi.stackexchange.com/questions/26829/…
    – goobering
    Commented Jul 19, 2016 at 10:02
  • Thanks @jogco I actually found some helpful info by googling that.
    – Iwotastic
    Commented Jul 19, 2016 at 13:55
  • Depending on the server that Flask is running under, it may be using multiple processes. Bear in mind that the camera firmware only permits a single process to own the camera at any one time. I don't think that's the issue here because it's complaining about the null-sink (if multiple processes try to access the camera, the error occurs when creating the MMAL component representing the camera). Still - you'll need to be very careful that only a single process owns the camera at one time.
    – Dave Jones
    Commented Jul 19, 2016 at 14:15
  • @Iwotastic, great! That was my intention. :)
    – jogco
    Commented Jul 19, 2016 at 20:59

3 Answers 3


I struggled for some time with this exact same problem. I believe that I've solved it, though I have to be honest, I don't understand exactly why it works. The key was turning off debugging in Flask. I got a hint at doing it by the comments on this page Video Streaming with Flask

if __name__ == "__main__":
    app.run(host='', port=8000, debug=False)

I've verified it by running the exact code with the debug flag set to True (and I get the mmal error) and set to False and everything works as expected. I hope it helps someone

  • 1
    Crazy! I have exactly the same problem and that fixed it!! Cheers bud
    – Lxrd-AJ
    Commented Jan 25, 2019 at 14:43

Well i've same issue, add camera.close() and work!

def getImage():
     return send_file("./templates/image.jpg")

I ended in the same pitfall.

I got a several threads application using opencv and picamera.

Without flask => no problem

With flask launched => out of ressources

Flask alone in a seperated app handling the camera only => nice streaming.

So even with one python process handling flask and threads, the camera crash.

I'm suspecting flask to have clever thread/gevent in background which causes this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.