在Luigi Orchestrator Task的run函数中使用luigi.DateParameter

时间:2015-12-03 17:51:35

标签: python orchestration luigi

我有使用Luigi Orchestrator的以下python代码。

class AggregateArtists(luigi.Task):
    date = luigi.DateParameter(default=date.today() - timedelta(days=1))

    def requires(self):
        return []

    def run(self):
        ...

我想在run()函数中使用date参数。问题是我不知道它是什么类型。在文档中,似乎此参数是datetime.date,因此我应该能够使用方法self.date.strftime()。但是这种方法对于DateParameters是不可行的。

我的问题是:

  • 如何在运行函数中使用代码的变量日期?它是什么类型的?字符串,datetime.date还是别的什么?

  • 在某些时候,我需要将此日期转换为YYYYMMDD形式的字符串,我该怎么办?

1 个答案:

答案 0 :(得分:1)

您的代码不完整,但我猜其余的代码如下。您必须在某处出错,因为它可以正常工作:DateParameter返回一个python日期值。请参阅luigi source code for details

我的tasks/foo.py

from datetime import date, timedelta
import luigi


class AggregateArtists(luigi.Task):
    date = luigi.DateParameter(default=date.today() - timedelta(days=1))

    def output(self):
        return luigi.LocalTarget("/tmp/foobar.txt")

    def run(self):
        with self.output().open('w') as out_file:
            out_file.write(self.date.strftime("%Y%m%d") + "\n")


if __name__ == "__main__":
    luigi.run()

运行任务:

$ python tasks/foo.py AggregateArtists --local-scheduler

DEBUG: Checking if AggregateArtists(date=2015-12-03) is complete
INFO: Scheduled AggregateArtists(date=2015-12-03) (PENDING)
INFO: Done scheduling tasks
INFO: Running Worker with 1 processes
DEBUG: Asking scheduler for work...
DEBUG: Pending tasks: 1
INFO: [pid 21831] Worker Worker(salt=179482616, workers=1, host=matagus-laptop, username=matagus, pid=21831) running   AggregateArtists(date=2015-12-03)
INFO: [pid 21831] Worker Worker(salt=179482616, workers=1, host=matagus-laptop, username=matagus, pid=21831) done      AggregateArtists(date=2015-12-03)
DEBUG: 1 running tasks, waiting for next task to finish
DEBUG: Asking scheduler for work...
INFO: Done
INFO: There are no more tasks to run at this time
INFO: Worker Worker(salt=179482616, workers=1, host=matagus-laptop, username=matagus, pid=21831) was stopped. Shutting down Keep-Alive thread

打印输出文件的内容:

$ cat /tmp/foobar.txt 
20151203