1

Here is my code:

from math import sqrt
from joblib import Parallel, delayed
import multiprocessing

def parallel_calc():
    if __name__ == '__main__':
        result = Parallel(n_jobs=2)(delayed(sqrt)(i ** 2) for i in range(10))
        return result

result = parallel_calc()
print(result[-1])

It generates error message: print(result[-1]) TypeError: 'NoneType' object is not subscriptable. In addition, it does not terminate.

Since I am on Window 7, I have to use this check if __name__ == '__main__':, but how do I get result from parallel_calc function?

user1700890
  • 7,144
  • 18
  • 87
  • 183

1 Answers1

2

Move the if __name__..., or boilerplate, outside of your function scope.

if __name__ == '__main__':
    def parallel_calc():
        result = Parallel(n_jobs=2)(delayed(sqrt)(i ** 2) for i in range(10))
        return result

    result = parallel_calc()
    print(result[-1])

Since your parallel_calc function does not reside within the boilerplate, it is being called twice.

The error is coming from the Parallel process not being executed the first time it is called, returning None

Take a look at "Boilerplate code in Python" for more information on the boilerplates

Community
  • 1
  • 1
Wondercricket
  • 7,651
  • 2
  • 39
  • 58