I'm trying to make a camera application with my Raspberry Pi that should launch on RPi startup. The program itself works fine. My problem is I'm getting the error
picamera.exc.PiCameraMMALError: Failed to enable connection: Out of resources
Now I know that this error is due to the RPi using the camera already in another instance. But here lies a problem I'm unable to find an answer to: The aim is to use the RPi headless i.e. without a display or keyboard etc. When I unplug the RPi from a power source, the camera instance seems to stay in some memory or at least it is not closed.
If I'm successful at executing my app properly on startup and unplug the RPi from its power source, the app will not run on the next startup and will instead result in the error above. My best solution so far is to start up the RPi, shut it down manually (reboot won't do it) and unplug + replug the power, after which the old camera instance does not exist anymore.
I cannot create a new camera instance and try to shut all camera instances down that way, because the error above is the result of trying to create the new camera instance.
Is there a way to programmatically "release" all current camera instances without referring directly to them (in Python)? The other alternative is to run a Raspbian bash/shell/etc. script on startup before running the application itself. If this is the only or more feasible option, I'm up to it.
I'm currently using the imutils.video module for interacting with the PiCamera, but if there is some other package that offers the utility I'm looking for, I'm up for the change.