Skip to main content
 首页 » 编程设计

python之如何在运行 local-scheduler 时创建 Luigi 依赖关系图

2024年08月26日32傻小

我已经在luigi框架内实现了一些任务,现在我想可视化依赖关系图。

我的同事指示我开始 luigid

luigid mkdir -p luigi;--background --logdir luigi --state-path luigi/state --address 0.0.0.0 --port 32145 

之后,我可以在 chrome 浏览器 localhost:32145 中看到任务可视化工具

然后我运行 luigi 任务

python luigi_scheduler.py --local-scheduler 

luigi_scheduler 通过以下行运行任务

luigi.run(main_task_cls = someTaskA) 

然后我收到以下消息,任务完成。然而,任务可视化工具上什么也没有出现。

DEBUG: Checking if get_usr_activity_all() is complete 
/usr/local/lib/python2.7/dist-packages/luigi/task.py:433: UserWarning: Task get_usr_activity_all() without outputs has no custom complete() method 
warnings.warn("Task %r without outputs has no custom complete() method" % self) 
INFO: Scheduled get_usr_activity_all() (PENDING) 
DEBUG: Checking if get_usr_hw(n=33000) is complete 
INFO: Scheduled get_usr_hw(n=33000) (DONE) 
DEBUG: Checking if get_usr_login(n=33000) is complete 
INFO: Scheduled get_usr_login(n=33000) (DONE) 
INFO: Done scheduling tasks 
INFO: Running Worker with 1 processes 
DEBUG: Asking scheduler for work... 
DEBUG: Pending tasks: 1 
INFO: [pid 16937] Worker Worker(salt=675435319, host=ubuntu, username=junchen, pid=16937) running   get_usr_activity_all() 
INFO: [pid 16937] Worker Worker(salt=675435319, host=ubuntu, username=junchen, pid=16937) done      get_usr_activity_all() 
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=675435319, host=ubuntu, username=junchen, pid=16937) was stopped. Shutting down Keep-Alive thread 

请您参考如下方法:

我知道这是一个老问题,但对于任何登陆那里的人来说,实际上有一种方法可以独立于任何调度程序来可视化任务的依赖关系树,由 luigi.tools.deps_tree 提供。模块。

假设您运行 luigi 时:

luigi.run(main_task_cls = someTaskA) 

您可以通过在运行前添加以下内容来让您的脚本打印 someTaskA 的依赖树:

import luigi.tools.deps_tree as deps_tree 
 
print(deps_tree.print_tree(someTaskA)) 

这将在控制台打印类似这样的内容:

└─--[Foo-{} (PENDING)] 
   |--[Bar-{'num': '0'} (PENDING)] 
   |  |--[Bar-{'num': '4'} (PENDING)] 
   |  └─--[Bar-{'num': '5'} (PENDING)] 
   |--[Bar-{'num': '1'} (PENDING)] 
   └─--[Bar-{'num': '2'} (PENDING)] 
      └─--[Bar-{'num': '6'} (PENDING)] 
         |--[Bar-{'num': '7'} (PENDING)] 
         |  |--[Bar-{'num': '9'} (PENDING)] 
         |  └─--[Bar-{'num': '10'} (PENDING)] 
         |     └─--[Bar-{'num': '11'} (PENDING)] 
         └─--[Bar-{'num': '8'} (PENDING)] 
            └─--[Bar-{'num': '12'} (PENDING)] 

其中 Foo 和 Bar 是任务名称,dicts {'num': 'x'} 是这些任务的参数.

我发现这特别有用,特别是对于调试具有大量参数的复杂工作流程。