给定一个类和其中的两个方法A、B。
当我在A类中尝试执行以下命令时,会出现“TypeError: can't pickle instancemethod objects”的错误:
joblib.Parallel(n_jobs=-1)(joblib.delayed(self.B)(arg1, arg2, arg3) for arg1 in arg1_values_list)
我在Stackoverflow上发现了几个有关 instancemethod 的帖子,其中一些甚至涉及到 joblib,但我仍然没有成功地克服这个错误。
编辑:
以下是代码:
from sklearn.externals.joblib import Parallel, delayed
def a(self, x):
print x
return x*10
class c(object):
def b(self):
Parallel(n_jobs=-1)(
delayed(a)(self, i)
for i in range(10))
test_c = c()
test_c.b()
正常运行时可正常运行。
但在尝试使用 "python -m cProfile" 进行分析时会失败。
n_jobs>1
的joblib时,只需不进行分析。cProfile无法分析子进程中发生的情况,因此尝试也是无用的。 - ogrisel