0

I'm trying to understand Python's Multi-processing module using the below given sample example but never been successful so far. I'm running the code in Spyder and it always hangs there with no output in the console. I learnt in some article that multiprocessing module doesn't work in Spyder console, so I created an exe out of it and executed in cmd but my VDI crashed and couldn't connect for hours until many attempts to restart. can I get suggestions on what I should do to make the below code run !

from multiprocessing import Pool

def f(x):
    return x*x

if __name__ == '__main__':
    p = Pool(5)
    print(p.map(f, [1, 2, 3]))
3
  • You have to find how many CPU core supported in your system Commented Dec 9, 2019 at 9:38
  • If it's not for appeasing VDI, you don't need to make it an .exe to run it from terminal (cmd). Just start it from the folder with python yourscript.py. For making an .exe you would have to include multiprocessing.freeze_support() Commented Dec 9, 2019 at 14:24
  • 1
    (Spyder maintainer here) This problem was fixed a long time, so I guess you're using a very old Spyder version (two or more years old). So please update and try again. Commented Dec 9, 2019 at 15:45

1 Answer 1

0

I copy-pasted your code into a file, named it tmp.py, and ran it in the console using:

python3 tmp.py

I got the following correct output:

[1, 4, 9]

It doesn't seem to me that there is anything wrong with your code. Your usage of Pool seems correct to me.

Sign up to request clarification or add additional context in comments.

2 Comments

It worked for me also, once I copy it to a file and tried in console but any reason it didn't work as is in console? And, I created exe of the same and didn't work again. Any reason?
I honestly don't know why but the problem is known! If you look at this SO thread: stackoverflow.com/questions/48078722/… it clearly states that Spyder on windows has issues with multiprocessing. As for the compilation issue how do you compile the script to exe?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.