延迟函数的解包结果[英] Unpacking result of delayed function

本文是小编为大家收集整理的关于延迟函数的解包结果的处理方法,想解了延迟函数的解包结果的问题怎么解决?延迟函数的解包结果问题的解决办法?那么可以参考本文帮助大家快速定位并解决问题。

问题描述

在使用延迟转换程序时,我偶然发现了一种常用的编程模式,该模式无法延迟.示例:

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()

这是预期的行为吗?

推荐答案

使用推荐答案

使用nout=关键字. Noreferrer">延迟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.

本文地址:https://www.itbaoku.cn/post/1793924.html