我正在尝试学习该模块作为pythonjoblib
中内置模块的替代方案。multiprocessing
我习惯于multiprocessing.imap
在可迭代对象上运行函数并在结果进入时返回结果。在这个最小的工作示例中,我不知道如何使用 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
哪个打印:
Inside function 0
Inside function 1
Inside function 2
Outside function 0
Outside function 1
Outside function 2
我想看看输出:
Inside function 0
Outside function 0
Inside function 1
Outside function 1
Inside function 2
Outside function 2
或类似的东西,表明迭代MP(...)
没有等待所有结果完成。对于更长的演示更改n_jobs=-1
和range(100)
.