Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What to do when I don't want Luigi to output a file but show the task as complete?

When looping over files with Luigi I do not what to be forced to save empty files just to show that the task was complete, and let the next task check if there are any rows in the txt, etc.

How can I have a task showing it succeeded (i.e. the run method worked as expected) without outputting a file? Am I missing something here?

like image 486
George Pamfilis Avatar asked May 11 '18 05:05

George Pamfilis


People also ask

What is Luigi Python?

Luigi is a Python package that manages long-running batch processing, which is the automated running of data processing jobs on batches of items. Luigi allows you to define a data processing job as a set of dependent tasks. For example, task B depends on the output of task A.


1 Answers

You can override the complete function.

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

The complete() function is looking at the output() function, by overriding complete() you can pass on any output and write your on complete condition.

Notice that if your complete function depends on the run function it may not be skipped..

like image 92
Anton Gellashvili Avatar answered Oct 13 '22 00:10

Anton Gellashvili