0

I'm trying to run a simple multiprocessing system on a Jetson NANO device, flashed with Jetpack 4.5. I'm doing what I would usually do on a computer, so I have a main script, launcher.py

launcher.py

import multiprocessing as mp
from multiprocessing import set_start_method, Queue, Event
from camera_reader import Camera_Reader_Initializer

def main():
    set_start_method("spawn")
    cam_read = mp.Process(target=Camera_Reader_Initializer, args=())
    cam_read.daemon = True
    cam_read.start()

if __name__ == "__main__":
    main()

which should launch the script camera.py (actually, together with a couple of other scripts) camera.py:

camera.py

print("check 00")

def Camera_Reader_Initializer():
    print('check 01')
    cam_read = Camera_Reader()
    cam_read.run()


class Camera_Reader():
    def __init__(self):
        print('check 02)
        self.source = "/dev/video0"

    def run(self):
        print('check 03')
        input = jetson.utils.videoSource(self.source)
        output = jetson.utils.videoOutput("")
        while output.IsStreaming():
            image = input.Capture(format='rgb8')
            output.Render(image)
            output.SetStatus(f"Video Viewer | {image.width:d}x{image.height:d} | {output.GetFrameRate():.1f} FPS")

However, when running launcher.py the only output I got is:

check 00

So, basically the cam_read object isn't created or run. Am I doing something wrong?

Carlo
  • 1,321
  • 12
  • 37
  • 1
    this should "fail" on any computer. the main process will immediately exit after starting the process, and the main process will send `SIGTERM` to any child processes marked `daemon`. What you have is effectively a race to see what can get printed out by the new process before it's `terminate()`'d – Aaron Aug 26 '21 at 14:44
  • Damn, that was stupid of me! :( You're right, there was supposed to be another part in the main process, this way everything just ends immediately. I was adapting an existing script to my needs now, but I ended up removing too much stuff. Thanks a lot! – Carlo Aug 26 '21 at 15:12

1 Answers1

1

The functionality of setting Process.daemon = True will cause the main process to call Process.terminate() when it exits. This is meant for long running child processes that can handle being closed without warning (in general you should handle the SIGTERM signal to cleanup before exiting).

Your main function in "launcher.py" does not wait for the child to do anything, and tries to exit basically right away. There seems to be just enough time for the child to get to the print("check 00") line before it's killed. This may seem somewhat consistent, but it should not be counted on even printing that. It's a race between how much the child can accomplish before the main process gets around to closing it.

Fixing this will depend on how you want it to function. If you still want the child to just run in the background forever until the main process exits, you need to make sure the main process takes some time (perhaps with time.sleep). If the child is completing some finite job, it probably shouldn't be a daemon, and you should call Process.join() on it to wait for it to finish at some point in main().

Aaron
  • 10,133
  • 1
  • 24
  • 40
  • 1
    Yes, the child process is reading frames from an IP camera and sending them to another process for elaboration. I generally have an infinite loop within the main process, and I set an event in the child process when the video stream ends to stop everything. But again, this time I forgot to add the loop. – Carlo Aug 26 '21 at 17:06
  • I wrote this answer right away, then stepped away from my computer for a bit before clicking sumbit... I saw your earlier comment afterwards – Aaron Aug 26 '21 at 17:08