8

我在 Luigi 中建立了一个任务管道。由于此管道将在不同的上下文中使用,因此可能需要在管道的开头或结尾包含更多任务,甚至需要在任务之间包含完全不同的依赖关系。

那时我想:“嘿,为什么要在我的配置文件中声明任务之间的依赖关系?”,所以我在我的 config.py 中添加了这样的内容:

PIPELINE_DEPENDENCIES = {
     "TaskA": [],
     "TaskB": ["TaskA"],
     "TaskC": ["TaskA"],
     "TaskD": ["TaskB", "TaskC"]
}

我对在整个任务中堆积的参数感到恼火,所以在某些时候我只引入了一个参数 ,task_config每个参数Task都有,并且存储了每个必要的信息或数据run()。所以我PIPELINE_DEPENDENCIES直接放在那里。

最后,我会让Task我定义的每一个都继承自luigi.Task一个自定义的 Mixin 类,它将实现 dynamic requires(),它看起来像这样:

class TaskRequirementsFromConfigMixin(object):
    task_config = luigi.DictParameter()

    def requires(self):
        required_tasks = self.task_config["PIPELINE_DEPENDENCIES"]
        requirements = [
            self._get_task_cls_from_str(required_task)(task_config=self.task_config)
            for required_task in required_tasks
        ]
        return requirements

    def _get_task_cls_from_str(self, cls_str):
        ...

不幸的是,这不起作用,因为运行管道给了我以下信息:

===== Luigi Execution Summary =====

Scheduled 4 tasks of which:
* 4 were left pending, among these:
    * 4 was not granted run permission by the scheduler:
        - 1 TaskA(...)
        - 1 TaskB(...)
        - 1 TaskC(...)
        - 1 TaskD(...)

Did not run any tasks
This progress looks :| because there were tasks that were not granted run permission by the scheduler

===== Luigi Execution Summary =====

和很多

DEBUG: Not all parameter values are hashable so instance isn't coming from the cache

虽然我不确定这是否相关。

所以: 1. 我的错误是什么?它可以修复吗?2.还有其他方法可以实现吗?

4

1 回答 1

1

我意识到这是一个老问题,但我最近学会了如何启用动态依赖项。我能够通过使用 WrapperTask 并使用我想传递给 requires 方法中的其他任务的参数产生一个 dict 理解(尽管如果你愿意,你也可以做一个列表)来实现这一点。

像这样的东西:

class WrapperTaskToPopulateParameters(luigi.WrapperTask):
    date = luigi.DateMinuteParameter(interval=30, default=datetime.datetime.today())

    def requires(self):
    base_params = ['string', 'string', 'string', 'string', 'string', 'string']
    modded_params = {modded_param:'mod' + base for base in base_params}
    yield list(SomeTask(param1=key_in_dict_we_created, param2=value_in_dict_we_created) for key_in_dict_we_created,value_in_dict_we_created in modded_params.items())

如果有兴趣,我也可以使用列表理解发布一个示例。

于 2021-09-24T21:24:07.000 回答