Python Multi Processing

114 Views Asked by At

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]))
1

There are 1 best solutions below

2
On

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.