Skip to main content
 首页 » 编程设计

python之如何将多个参数传递给 Luigi 子任务

2025年05月04日63tintown

我有一个 Luigi 任务 requires一个子任务。子任务取决于父任务传递的参数(即执行 require ing 的任务)。我知道您可以通过设置指定子任务可以使用的参数...

def requires(self): 
    return subTask(some_parameter) 

...然后在子任务上,通过设置...接收参数

x = luigi.Parameter() 

虽然这似乎只允许您传递一个参数。通过任意数量的参数发送我想要的任何类型的最佳方法是什么?我真的想要这样的东西:

class parentTask(luigi.Task): 
 
    def requires(self): 
        return subTask({'file_postfix': 'foo', 
                        'file_content': 'bar' 
        }) 
 
    def run(self): 
        return 
 
 
class subTask(luigi.Task): 
    params = luigi.DictParameter() 
 
    def output(self): 
        return luigi.LocalTarget("file_{}.csv".format(self.params['file_postfix'])) 
 
    def run(self): 
        with self.output().open('w') as f: 
            f.write(self.params['file_content']) 

如您所见,我尝试使用 luigi.DictParameter而不是直线 luigi.Parameter但我得到 TypeError: unhashable type: 'dict'当我运行上面的代码时,来自 Luigi 内心深处的某个地方。

运行 Python 2.7.11,Luigi 2.1.1

请您参考如下方法:

What is the best way to send through an arbitrary number of parameters, of whatever types I want?

最好的方法是使用命名参数,例如,

#in requires 
return MySampleSubTask(x=local_x, y=local_y) 
 
class MySampleSubTask(luigi.Task): 
    x = luigi.Parameter() 
    y = luigi.Parameter()