Search code examples
pythonmultiprocessingnvidia-jetsonnvidia-jetson-nano

Multiprocessing on Jetson NANO


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?


Solution

  • 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().