Skip to content Skip to sidebar Skip to footer

Multiprocessing A Loop Inside A Loop Inside A Function

I wrote some code to break up a for loop into multiple processes to speed up calculations. import numpy as np import formfactors from subdivide_loop import subdivide_loop import mu

Solution 1:

As the error suggests, make sure your outer_function() (or whatever initiates it) is called from within an __main__ guard, e.g.

if __name__ == "__main__":
    outer_function()

It doesn't have to be the outer_function() but you need to trace it all back to the first step that initializes the chain that ultimately leads to the call to multiprocessing.Process() and put it within the above block.

This is because on non-forking systems multiple processes are essentially run as subprocesses so creating new processes from the main script would end up with an infinite recursion/processes spawning. You can read more about it in this answer. Because of that, you have to make sure your multiprocessing initialization code executes only once which is where the __main__ guard comes in.

Post a Comment for "Multiprocessing A Loop Inside A Loop Inside A Function"