Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 14040

Parallel processing in python and need for if __name__ == '__main__'

$
0
0

I've been battling the need for parallel processing in my application. I have relatively good understanding that GIL prevents concurrent processing in a single interpreter, and that there can only be one interpreter per process. At least until 3.10 (I need to use 3.8 to integrate manufacturer's drivers). But my application is proving challenging.

My application requirements:

In my application we I'm creating an image acquisition module that will handle an arbitrary number of Flir cameras connected to a computer, for robotics and machine vision. This means the image acquisition module will run in parallel with other modules that control important things, such as motors and DAQs.

When initialized, the image acquisition module will check all cameras in the system to see which one has the name requested:

import ImageAcquisition as iaia.Initialize("cam1", "cam2") # Creates two parallel process. One for each camera.

For each camera a parallel process should be created to handle the image analysis associated with it. This means grabbing frames, displaying them, processing them, and also making some image analysis results available to the main process.

One important requirement is the module is self-contained. However, I'm finding that the if __name__ == "__main__": test is always needed in the program entry code (client code). This is because the new processes will run the main program on their interpreters as well.

Is there an alternative to having if __name__ == "__main__":? So I can make my module fully self-contained.

One alternative could be to use the subprocess library and run the process without the "spawn" mechanism that is used by multiprocessing library in Windows. But there is some overhead when it comes to sharing information between processes that is better handled in the multiprocessing library.

Another alternative is to inject the if __name__ == "__main__": in the user's strings, in our main application.


Viewing all articles
Browse latest Browse all 14040

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>