1

I had looked at the documentaion. And there was an example

from multiprocessing import Pool

def f(x):
    return x*x

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

The problem is: it is not working. I run this code in Jupiter Notebook cell. And this the cell doesn't raise any exception. But Jupiter's terminal does. And it says: AttributeError: Can't get attribute 'f' on <module '__main__' (built-in)>

As written here the problem may be because I don't use __name__ == '__main__' condition. But I do.

I had literally copy and paste example from the documention and it's not working. What should I do?

Yoskutik
  • 1,859
  • 2
  • 17
  • 43

1 Answers1

0

I suspect you are running on Windows. If so, this is a known issue. See this article. You need to add your function f to a file, such as worker.py:

worker.py

def f(x):
    return x*x

Then you jupyter notebook code becomes:

from multiprocessing import Pool
import worker


if __name__ == '__main__':
    with Pool(5) as p:
        print(p.map(worker.f, [1, 2, 3]))
Booboo
  • 38,656
  • 3
  • 37
  • 60