I have not been able to implement the suggestion here: Applying two functions to two lists simultaneously.
I guess it is because the module is imported by another module and thus my Windows spawns multiple python processes?
My question is: how can I use the code below without the if if __name__ == "__main__":
args_m = [(mortality_men, my_agents, graveyard, families, firms, year, agent) for agent in males]
args_f = [(mortality_women, fertility, year, families, my_agents, graveyard, firms, agent) for agent in females]
with mp.Pool(processes=(mp.cpu_count() - 1)) as p:
p.map_async(process_males, args_m)
p.map_async(process_females, args_f)
Both process_males
and process_females
are fuctions.
args_m, args_f
are iterators
Also, I don't need to return anything. Agents are class instances that need updating.
The idea of
if __name__ == '__main__':
is to avoid infinite process spawning.When pickling a function defined in your main script, python has to figure out what part of your main script is the function code. It will basically re run your script. If your code creating the
Pool
is in the same script and not protected by the "if main", then by trying to import the function, you will try to launch anotherPool
that will try to launch anotherPool
....Thus you should separate the function definitions from the actual main script:
The reason you need to guard multiprocessing code in a
if __name__ == "__main__"
is that you don't want it to run again in the child process. That can happen on Windows, where the interpreter needs to reload all of its state since there's nofork
system call that will copy the parent process's address space. But you only need to use it where code is supposed to be running at the top level since you're in the main script. It's not the only way to guard your code.In your specific case, I think you should put the
multiprocessing
code in a function. That won't run in the child process, as long as nothing else calls the function when it should not. Your main module can import the module, then call the function (from within anif __name__ == "__main__"
block, probably).It should be something like this:
some_module.py:
main.py:
In your real code you might want to pass some arguments or get a return value from
do_stuff
(which should also be given a more descriptive name than the generic one I've used in this example).