How to reset luigi task status?

前端 未结 4 1232
清歌不尽
清歌不尽 2021-02-19 20:36

Currently, I have a bunch of luigi tasks queued together, with a simple dependency chain( a -> b -> c -> d). d gets executed first, and

4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-19 21:04

    I typically do this by overriding complete():

    class BaseTask(luigi.Task):
    
        force = luigi.BoolParameter()
    
        def complete(self):
            outputs = luigi.task.flatten(self.output())
            for output in outputs:
                if self.force and output.exists():
                    output.remove()
            return all(map(lambda output: output.exists(), outputs))
    
    
    class MyTask(BaseTask):
        def output(self):
            return luigi.LocalTarget("path/to/done/file.txt")
    
        def run(self):
            with self.output().open('w') as out_file:
                out_file.write('Complete')
    

    When you run the task, the output file is created as expected. Upon instantiating the class with force=True, the output file will still exist until complete() is called.

    task = MyTask()
    task.run()
    task.complete()
    # True
    
    new_task = MyTask(force=True)
    new_task.output().exists()
    # True
    new_task.complete()
    # False
    new_task.output().exists()
    # False
    

提交回复
热议问题