I'm trying to learn the joblib
module as an alternative to the builtin multiprocessing
module in python. I'm used to using multiprocessing.imap
to run a function over an iterable and returning the results as they come in. In this minimal working example, I can't figure out how to do it with joblib:
import joblib, time
def hello(n):
time.sleep(1)
print "Inside function", n
return n
with joblib.Parallel(n_jobs=1) as MP:
func = joblib.delayed(hello)
for x in MP(func(x) for x in range(3)):
print "Outside function", x
Which prints:
Inside function 0
Inside function 1
Inside function 2
Outside function 0
Outside function 1
Outside function 2
I'd like to see the output:
Inside function 0
Outside function 0
Inside function 1
Outside function 1
Inside function 2
Outside function 2
Or something similar, indicating that the iterable MP(...)
is not waiting for all the results to complete. For longer demo change n_jobs=-1
and range(100)
.