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]))
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
  • You have to find how many CPU core supported in your system – WenJuan Dec 09 '19 at 09: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()`](https://stackoverflow.com/a/54066043/9059420) – Darkonaut Dec 09 '19 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. – Carlos Cordoba Dec 09 '19 at 15:45

1 Answers1

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.

alex_bits
  • 642
  • 1
  • 4
  • 14
  • 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? –  Dec 09 '19 at 13:33
  • I honestly don't know why but the problem is known! If you look at this SO thread: https://stackoverflow.com/questions/48078722/no-multiprocessing-print-outputs-spyder it clearly states that Spyder on windows has issues with multiprocessing. As for the compilation issue how do you compile the script to exe? – alex_bits Dec 09 '19 at 14:41