Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: joblib does not work on custom-defined function

I am trying to use parallel computation package joblib in python. I can execute the following example and get the result

Parallel(n_jobs=8)(delayed(sqrt)(i) for i in range(10))

However, the following code does not work out.

from joblib import Parallel, delayed
def f(x):
    return 1
def y(x):
    result=Parallel(n_jobs=8)(delayed(x)(i) for i in range(10))
    return result
if __name__ == '__main__':
    print y(f)

when I run the above code, it keeps running forever without generating any result or message.

Can anyone figure out the reason for this strange behavior(I am using windows)? thanks

like image 412
3c. Avatar asked Dec 04 '25 15:12

3c.


1 Answers

The problem is in this line of code:

result=Parallel(n_jobs=8)(delayed(x)(i) for i in range(10))

It should instead be:

result=Parallel(n_jobs=8)(delayed(f)(i) for i in range(10))

You have to pass the function name to delayed, just like you did with the first example using the sqrt.

like image 189
whalsey Avatar answered Dec 07 '25 15:12

whalsey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!