• Device creation in a python subprocess freezes

Hello,

while the device creation in the main thread or in a thread of the main process works fine, the program freezes if this is done in a subprocess (created through the python multiprocessing module).

Is this the expected behavior?

Thanks!

BR

Alessandro

code mwe

Hi abroekhof , in the link above there is the code of the MWE.

If the acquisition_oak function is executed within a thread (line 234) it works fine, instead if within a process (line 235) it hangs at line 134 (with dai.Device(pipeline) as device🙂.

Cheers.

  • erik replied to this.
    13 days later

    Hi AlessandroB ,
    I believe that connecting to the device has to be done in the main thread, and there must be some delay between connecting to 2 separate devices. We have a demo here that uses threading. Thoughts?
    Thanks, Erik
    PS. note that 250 LoC isn't very minimal🙂

      Hi erik ,

      thank you for the clarification, it wasn't clear to me the main thread "constraint".

      Sorry for the MWE length, I'll do better in the future.

      Cheers

      Alessandro

      6 days later

      Hi erik ,

      I had a think and the issue is slightly different.

      I'm able to initialize a device in a thread different from the main thread, but I fail if that happens within a different process. Instead, @abroekhof claims he's able to do so.

      In my application I found a workaround, nonetheless it would be interesting to get to know if initialization within a subprocess is actually allowed.

      Best

      Alessandro

      Hi @AlessandroB we're able to connect to cameras in separate processes, looks something like this.

      for info in infos:
          # Converts enum eg. 'XLinkDeviceState.X_LINK_UNBOOTED' to 'UNBOOTED'
          state = str(info.state).split('X_LINK_')[1]
          logging.info(
              'Found device %s, MxId: %s, State: %s', info.name, info.mxid, state
          )
          proc = mp.Process(
              target=cam_process,
              name=info.mxid,
              args=(
                  log_queue,
                  info.mxid,
                  data_queue,
                  exit_event,
              ),
              daemon=True,
          )
          procs.append(proc)
          proc.start()

        Hi abroekhof ,

        if I run the function in a thread it works, if in a process it hangs at the line "with dai.Device(pipeline) as device:".

        It's very strange, as it works for you!

        Cheers

        Alessandro

        Oh, one other thing to try, put

        mp.set_start_method('spawn')

        at the top of your main function.