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.

Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284
user6228086
  • 31
  • 1
  • 3
  • 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? – Akshat Mahajan Apr 20 '16 at 04:23

1 Answers1

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
Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284
  • This works for me. Any idea why it would not work in Jupyter notebook or when typing in commands in a shell? – gregorio099 May 12 '20 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: https://stackoverflow.com/questions/47313732/jupyter-notebook-never-finishes-processing-using-multiprocessing-python-3 . It involves saving the function in a separate .py file and then importing it. – gregorio099 May 12 '20 at 22:06