1

**********First problem************

The following code doesn't work in a Windows computer (python 3.7, Jupyter notebook)- nothing happens, the kernel keeps being busy until I restart it, and nothing is printed. BUT it does work on a Linux computer (python 3.6.9, Jupyter notebook).

import multiprocessing as mp

def f(x):
    print(x)
    return x*x

if __name__ == '__main__':
    with mp.Pool(mp.cpu_count()) as p:
        print(p.map(f, [1, 2, 3]))

****Partial solution and a following question*****

As suggested in the first comment here, saving f(x) in a different py file partially solved it: it now prints [1, 4, 9] but does not perform the print(x) inside f(x). Any suggestions?

HBi
  • 59
  • 6
  • You say "this basic code doesn't work", what does it do? Since you're using Jupyter, the `if __name__ == "__main__"` test, to my understanding, is not required. – dspencer Mar 14 '20 at 13:06
  • Nothing happens, the kernel keeps being busy until I restart it, and nothing is printed. Also- pls check my second edit. @dspencer – HBi Mar 16 '20 at 06:30
  • How about the top answer [here](https://stackoverflow.com/questions/47313732/jupyter-notebook-never-finishes-processing-using-multiprocessing-python-3)? This seems to be the same behaviour you're having on Windows. – dspencer Mar 16 '20 at 06:34
  • Wow! The solution you offered made it work! Thank you @dspencer!!! However, it does not print from inside the function, for example- if I had print(x) inside f(x) (which is now on a different file) it would not print anything. Any idea how to solve this? – HBi Mar 17 '20 at 07:54
  • I recommend you open a new question if you have a problem you can't solve by research; you should avoid asking new questions in the comments section. – dspencer Mar 17 '20 at 07:58
  • Please check my edit @dspencer – HBi Mar 17 '20 at 08:27

0 Answers0