当我不希望 Luigi 输出文件但显示任务已完成时该怎么办?

Geo*_*lis 6 python pipeline luigi

当使用 Luigi 循环文件时,我不会强制保存空文件只是为了表明任务已完成,并让下一个任务检查 txt 中是否有任何行,等等。

如何在不输出文件的情况下让任务显示它成功(即运行方法按预期工作)?我在这里错过了什么吗?

小智 7

您可以覆盖完整的功能。

class LuigiTaskB(luigi.Task):
    def run(self):
        print "running task b"
        with self.output().open('w') as out_file:
            print >> out_file, "some text"

    def output(self):
        return luigi.LocalTarget("somefile")


class LuigiTaskA(luigi.Task):
    task_complete = False
    def requires(self):
        return LuigiTaskB()

    def run(self):
        print "running task a"
        self.task_complete = true

    def complete(self):
        # Make sure you return false when you want the task to run.
        # And true when complete

        return  self.task_complete
        # This will out put :
        #   running task b
        #   running task a
        # And this on the second time you'll run:
        #   running task a
Run Code Online (Sandbox Code Playgroud)

complete() 函数正在查看 output() 函数,通过覆盖 complete() 您可以传递任何输出并编写完整条件。

请注意,如果您的完整函数依赖于 run 函数,则可能不会跳过它。


Gar*_*Cat 5

你也可以像这样使用 MockTarget:

class MyTask(luigi.Task):
    def requires(self):
        ...
    def run(self):
        ...
    def output(self):
        return MockFile("MyTask", mirror_on_stderr=True) # check that MyTask in quotes
Run Code Online (Sandbox Code Playgroud)