0

I took example from joblib tutorial. Here is how my code looks like:

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

test = Parallel(n_jobs=2)(delayed(sqrt)(i ** 2) for i in range(10))
print(test)

It produces the following error message:

Attempting to do parallel computing without protecting your import
on a system that does not support forking. To use parallel-computing 
in a script, you must protect you main loop using 
"if __name__ == '__main__'". Please see the joblib documentation on 
Parallel for more information

And it runs for too long. What am I missing?

user1700890
  • 7,144
  • 18
  • 87
  • 183

2 Answers2

4

What the error message and BrenBran are telling you is that a) you should read the error message, and b) you should organise your code something like:

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

if __name__ == '__main__':
    test = Parallel(n_jobs=2)(delayed(sqrt)(i ** 2) for i in range(10))
    print(test)

HTH barny

0

I had to add backend="threading" to barny's code to run without error:

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

if __name__ == '__main__':
    test = Parallel(n_jobs=2, backend="threading")(delayed(sqrt)(i ** 2) for i in range(10))
print(test)
P__2
  • 66
  • 5
  • 1
    I was reviewing the docs and if we use backend="threading" then it may lead to Global interpreter lock of python. – Aman Raparia Dec 07 '18 at 07:49