我试图学习joblib
模块作为python中内置multiprocessing
模块的替代方案。我曾经习惯使用multiprocessing.imap
在一个iterable上运行一个函数并返回结果。在这个最小的工作示例中,我无法弄清楚如何使用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)
。
答案 0 :(得分:4)
stovfl的回答很优雅,但仅适用于首批派发的产品。在该示例中,它之所以有效,是因为工人从不挨饿(n_tasks < 2*n_jobs
为了使这种方法起作用,还必须调用最初传递给apply_async
的回调。这是BatchCompletionCallBack
的实例,它计划要处理的下一批任务。
一种可能的解决方案是将任意回调包装在可调用对象中,如下所示(在joblib == 0.11,py36中测试):
from joblib._parallel_backends import MultiprocessingBackend
from joblib import register_parallel_backend, parallel_backend
from joblib import Parallel, delayed
import time
class MultiCallback:
def __init__(self, *callbacks):
self.callbacks = [cb for cb in callbacks if cb]
def __call__(self, out):
for cb in self.callbacks:
cb(out)
class ImmediateResultBackend(MultiprocessingBackend):
def callback(self, result):
print("\tImmediateResult function %s" % result)
def apply_async(self, func, callback=None):
cbs = MultiCallback(callback, self.callback)
return super().apply_async(func, cbs)
register_parallel_backend('custom', ImmediateResultBackend)
def hello(n):
time.sleep(1)
print("Inside function", n)
return n
with parallel_backend('custom'):
res = Parallel(n_jobs=2)(delayed(hello)(y) for y in range(6))
输出
Inside function 0
Inside function 1
ImmediateResult function [0]
ImmediateResult function [1]
Inside function 3
Inside function 2
ImmediateResult function [3]
ImmediateResult function [2]
Inside function 4
ImmediateResult function [4]
Inside function 5
ImmediateResult function [5]
答案 1 :(得分:2)
获取来自joblib的立即结果,例如:
from joblib._parallel_backends import MultiprocessingBackend
class ImmediateResult_Backend(MultiprocessingBackend):
def callback(self, result):
print("\tImmediateResult function %s" % (result))
# Overload apply_async and set callback=self.callback
def apply_async(self, func, callback=None):
applyResult = super().apply_async(func, self.callback)
return applyResult
joblib.register_parallel_backend('custom', ImmediateResult_Backend, make_default=True)
with joblib.Parallel(n_jobs=2) as parallel:
func = parallel(delayed(hello)(y) for y in range(3))
for f in func:
print("Outside function %s" % (f))
<强>输出强>:
注意:我在time.sleep(n * random.randrange(1,5))
中使用def hello(...)
,因此processes
已准备就绪。
内部功能0
内部功能1
ImmediateResult函数[0]
内部功能2
ImmediateResult函数[1]
ImmediateResult函数[2]
外部功能0
外部功能1
外部功能2
使用Python测试:3.4.2 - joblib:0.11
答案 2 :(得分:-1)
>>> 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)
... res = MP(func(x) for x in range(3)) # This is not an iterator.
...
Inside function 0
Inside function 1
Inside function 2
>>> type(res)
<type 'list'>
您正在处理的不是发电机。因此,您不应期望它会为您提供中间结果。我在文档中看到的任何内容似乎都没有提及(或者我没有阅读相关部分)。
欢迎您阅读文档并搜索“中级”结果主题: https://pythonhosted.org/joblib/search.html?q=intermediate&check_keywords=yes&area=default
我的理解是每次调用parallel
都是一个障碍,为了得到中间结果,你需要对处理进行分块:
>>> 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 chunk in range(3):
... x = MP(func(y) for y in [chunk])
... print "Outside function", x
...
Inside function 0
Outside function [0]
Inside function 1
Outside function [1]
Inside function 2
Outside function [2]
>>>
如果你想获得技术,有一个回调机制,但它专门用于进度报告(BatchCompletionCallBack
),但你需要更多的代码更改。