该包joblib
有一个函数delayed
,它捕获传递给函数的参数。例如,它可以像这样使用:
from joblib import delayed
def f(n):
return n**2
delayed(f)(2)
# (<function f at 0x7f939eb3fe60>, (2,), {})
h = delayed(f)
h(2)
# (<function f at 0x7f939eb3fe60>, (2,), {})
后一种用法让我觉得这可以像任何其他装饰器一样使用。其文档支持这一点:
joblib.delayed(函数,check_pickle=True)
装饰器用于捕获函数的参数。
但是,这失败了:
from joblib import delayed
@delayed
def g(n):
return n**2
# Traceback (most recent call last):
# File "<stdin>", line 1, in <module>
# File "/usr/lib/python2.7/dist-packages/joblib/parallel.py", line 158, in delayed
# pickle.dumps(function)
# File "/usr/lib/python2.7/copy_reg.py", line 70, in _reduce_ex
# raise TypeError, "can't pickle %s objects" % base.__name__
#TypeError: can't pickle function objects
我试过这个Python 2.7.12