waveform80 / picamera

A pure Python interface to the Raspberry Pi camera module
https://picamera.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
1.57k stars 357 forks source link

PiCamera singleton pattern #641

Closed luxedo closed 4 years ago

luxedo commented 4 years ago

If we try to instantiate more than one picamera.PiCamera we 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))0x912f60 (ENOSPC)
mmal: mmal_connection_enable: output port couldn't be enabled

...
picamera.exc.PiCameraMMALError: Failed to enable connection: Out of resources

Would it be possible to use the singleton pattern and have always only one PiCamera instance? Is there any major drawbacks? One I can think right now is that changing the camera properties and init arguments may cause confusing behavior.

I'm curious about this because when I instantiate a camera object running a flask server in debug mode, It runs the code twice for some reason and then crashes because of the camera error. I solved by implementing the singleton myself.

6by9 commented 4 years ago

Surely that only helps against invoking a second instance within the same process. Have multiple processes and you're back in the same position again.