depthai-python icon indicating copy to clipboard operation
depthai-python copied to clipboard

getAllAvailableDevices doesn't work accross multiprocessing.Process

Open zrezke opened this issue 1 year ago • 0 comments

When spawning a process that calls dai.getAllAvailableDevices, and a process that opens and closes devices, the closed devices won't show up when calling dai.getAllAvailableDevices in the seperate process anymore, but will still show in the original process...

To reproduce run this script then press q, to close the device.
You should see:

  • Available devices in original process will include the closed device
  • Available devices from periodic_get_devices won't include the closed device
import cv2
import depthai as dai
import time
from multiprocessing import Process

def periodic_get_devices():
    while True:
        print("Available devices:", [d.getMxId()
              for d in dai.Device.getAllAvailableDevices()])
        time.sleep(2)

process = Process(target=periodic_get_devices)
process.start()
def handle_crash():
    process.terminate()
    exit(0)

pipeline = dai.Pipeline()
cam = pipeline.createColorCamera()
cam.setPreviewSize(300, 300)
cam.setInterleaved(False)
cam.setResolution(dai.ColorCameraProperties.SensorResolution.THE_1080_P)
cam.setFps(30)

out_cam = pipeline.createXLinkOut()
out_cam.setStreamName("cam")
cam.preview.link(out_cam.input)

try:
    while True:
        with dai.Device(pipeline) as device:
            out_queue = device.getOutputQueue(name="cam", maxSize=1, blocking=False)
            while True:
                cv2.waitKey(1)
                if out_queue.has():
                    cv2.imshow("Stream", out_queue.tryGet().getCvFrame())
                if cv2.waitKey(1) == ord('q'):
                    break
        print("Available devices in the original process:", [d.getMxId() for d in dai.Device.getAllAvailableDevices()])
        cv2.waitKey(10000) # Wait 10s to see if device will show up in periodic_get_devices
except:
    handle_crash()

zrezke avatar Apr 04 '23 12:04 zrezke