当我不想让Luigi输出一个文件,但显示任务已完成时该怎么办?

2024-04-20 09:18:43 发布

您现在位置:Python中文网/ 问答频道 /正文

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

如何让一个任务在不输出文件的情况下显示成功(即run方法按预期工作)?我是不是少了点什么?在


Tags: 文件方法runtxt情况luigi
2条回答

您还可以使用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

您可以重写整个函数。在

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

complete()函数查看output()函数,通过重写complete(),您可以传递任何输出并编写完成时的条件。在

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

相关问题 更多 >