Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
910 views
in Technique[技术] by (71.8m points)

python - Unpacking result of delayed function

While converting my program using delayed, I stumbled upon a commonly used programming pattern that doesn't work with delayed. Example:

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

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

Raises: TypeError: Delayed objects of unspecified length are not iterable While the following work around does not. But looks a lot more clumsy

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

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

Is this the intended behaviour?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use the nout= keyword as described in the delayed docstring

@delayed(nout=2)
def f(...):
    return a, b

x, y = f(1)

Docstring

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.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...