我希望在快速调用图中使下游函数接受多个参数,但是当我调用get
时,dask
说我缺少位置参数。
这里是Minimal, Reproducible Example:
>>> import dask.threaded
>>> dsk = {'in':2, 'f1': (lambda x:(x**2, x**3), 'in'), 'out':(lambda x2,x3:(x2,x3), 'f1')}
>>> dask.threaded.get(dsk, 'out')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/nathan/GLOBAL/lib/python3.6/site-packages/dask/threaded.py", line 76, in get
pack_exception=pack_exception, **kwargs)
File "/home/nathan/GLOBAL/lib/python3.6/site-packages/dask/local.py", line 462, in get_async
raise_exception(exc, tb)
File "/home/nathan/GLOBAL/lib/python3.6/site-packages/dask/compatibility.py", line 112, in reraise
raise exc
File "/home/nathan/GLOBAL/lib/python3.6/site-packages/dask/local.py", line 230, in execute_task
result = _execute_task(task, data)
File "/home/nathan/GLOBAL/lib/python3.6/site-packages/dask/core.py", line 119, in _execute_task
return func(*args2)
TypeError: <lambda>() missing 1 required positional argument: 'x3'
如何让一个函数在不使用*args
的情况下接受多个参数?
答案 0 :(得分:1)
输出(x**2, x**3)
是单个tuple
,不会自动解压缩;就像如果您将单个tuple
传递给具有多个参数的函数一样,则不会。
但是,如果您不喜欢*args
,则可以做的一件事是显式地将元组解包:
In [21]: def out(x):
...: x2, x3 = x
...: return (x2, 3*x3)
...:
In [22]: dsk = {'in': 2, 'f1': (lambda x:(x**2, x**3), 'in'), 'out': (out, 'f1')}
In [23]: dask.threaded.get(dsk, 'out')
Out[23]: (4, 24)
或者,如果您不介意只按索引访问元素:
In [27]: dsk = {'in': 2, 'f1': (lambda x: (x**2, x**3), 'in'), 'out': (lambda x: (x[0], 3*x[1]), 'f1')}
In [29]: dask.threaded.get(dsk, 'out')
Out[29]: (4, 24)