0

On Windows platform these two lines cause Python code execution to run in infinite loop with every loop starting from the beginning of the script:

import multiprocessing as mp manager=mp.Manager() 

It runs fine on Mac. What would be a reason? It successfully imports import multiprocessing as mp. But on manager=mp.Manager() it breaks (with no errors) and jumps back to the first line of the code.

1 Answer 1

2

On Windows, you have to protect calls to multiprocessing with if __name__ == "__main__":. See here for details on why this is required. This should work fine:

import multiprocessing as mp if __name__ == "__main__": manager=mp.Manager() 

Edit:

Note that putting your multiprocessing code inside functions you call from the if block is ok, too. Like this:

import multiprocessing as mp def func(x): print("Got %s" % (x,)) def main(): p = multiprocessing.Pool() results = p.map(func, range(0,5)) if __name__ == "__main__": manager = mp.Manager() main() 
Sign up to request clarification or add additional context in comments.

8 Comments

So I have to move import into if name==main scope?
@Sputnix No problem. Just note that all your calls that involve creating a new process via multiprocessing (like creating a Pool or Process) need to be done inside the if __name__ ... block.
Should I put pool.map_async(functName, ['arg1','arg2','arg3']) inside of if __name__=="__main__" too?
@Sputnix Yep, that too.
And where do I declare functName() function that is called by pool.map_async(functName, argList)..... inside of `if name__=="__main"? Isn't it a mess?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.