当任务依赖关系变得过时时,luigi可以重新运行任务吗?

Oph*_*tan 11 python luigi

据我所知,a luigi.Target既可以存在,也可以不存在.因此,如果luigi.Target存在,则不会重新计算.

我正在寻找一种方法来强制重新计算任务,如果其中一个依赖项被修改,或者其中一个任务的代码发生了变化.

J D*_*ith 19

实现目标的complete(...)一种方法是重写方法.

文档complete很简单.

只需实现一个检查约束的函数,False如果要重新计算任务,则返回.

例如,要在更新依赖项时强制重新计算,您可以执行以下操作:

def complete(self):
    """Flag this task as incomplete if any requirement is incomplete or has been updated more recently than this task"""
    import os
    import time

    def mtime(path):
        return time.ctime(os.path.getmtime(path))

    # assuming 1 output
    if not os.path.exists(self.output().path):
        return False

    self_mtime = mtime(self.output().path) 

    # the below assumes a list of requirements, each with a list of outputs. YMMV
    for el in self.requires():
        if not el.complete():
            return False
        for output in el.output():
            if mtime(output.path) > self_mtime:
                return False

    return True
Run Code Online (Sandbox Code Playgroud)

False当任何需求不完整或者任何修改比当前任务更新或当前任务的输出不存在时,这将返回.

检测代码何时更改更难.您可以使用类似的方案(检查mtime),但除非每个任务都有自己的文件,否则它会被命中.

由于能够覆盖complete,因此可以实现任何需要重新计算的逻辑.如果你想要一个特定的complete方法来完成很多任务,我建议你进行子类化luigi.Task,在complete那里实现你的自定义,然后从子类继承你的任务.