3
import multiprocessing as mul

def f(x):
    return x**2

pool = mul.Pool(5)
rel  = pool.map(f,[1,2,3,4,5,6,7,8,9,10])

print(rel)

When I run the program above, the application is stuck in a loop and can't stop. I am using python 3.5 in windows, is there something wrong?

This is what I see on my screen:

I am new to finance data analysis; and I am trying to find out a way to solve the big data problem with parallel computing.

1
  • Break it and try again. As written (and as I've just tested), this code should work flawlessly. It's probably not your code, it's the system you're using. What platform are you on? Apr 20, 2016 at 4:23

1 Answer 1

6

Its not working because you are typing the commands in a shell; try saving the code in a file and running it directly.

Don't forget to copy the code correctly, you were missing a very important if statement (see the documentation).

Save this to a file, for example example.py on the desktop:

import multiprocessing as mul

def f(x):
    return x**2

if __name__ == '__main__':
    pool = mul.Pool(5)
    rel  = pool.map(f,[1,2,3,4,5,6,7,8,9,10])

    print(rel)

Then, open a command prompt and type:

python %USERPROFILE%\Desktop\example.py
2
  • This works for me. Any idea why it would not work in Jupyter notebook or when typing in commands in a shell? May 12, 2020 at 21:16
  • I found one way to use pool.map in Jupyter notebook and with commands in a shell from the accepted answer here: stackoverflow.com/questions/47313732/… . It involves saving the function in a separate .py file and then importing it. May 12, 2020 at 22:06

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.