Search code examples
pythonjoblib

python simple parallel computation with joblib


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?


Solution

  • 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