解包延迟函数的结果

时间:2017-06-17 09:10:24

标签: python dask dask-delayed

在使用延迟转换我的程序时,我偶然发现了一种常用的编程模式,该模式并不适用于延迟。例如:

from dask import delayed
@delayed
def myFunction():
    return 1,2

a, b = myFunction()
a.compute()

加注:TypeError: Delayed objects of unspecified length are not iterable 虽然以下解决方法没有。但看起来更笨拙

from dask import delayed
@delayed
def myFunction():
    return 1,2

dummy = myFunction()
a, b = dummy[0], dummy[1]
a.compute()

这是预期的行为吗?

1 个答案:

答案 0 :(得分:6)

使用delayed docstring

中所述的nout=关键字
@delayed(nout=2)
def f(...):
    return a, b

x, y = f(1)

文档字符串

nout : int, optional
    The number of outputs returned from calling the resulting ``Delayed``
    object. If provided, the ``Delayed`` output of the call can be iterated
    into ``nout`` objects, allowing for unpacking of results. By default
    iteration over ``Delayed`` objects will error. Note, that ``nout=1``
    expects ``obj``, to return a tuple of length 1, and consequently for
    `nout=0``, ``obj`` should return an empty tuple.
相关问题