Python 在同时运行多个脚本时限制Luigi Worker的数量
据我所见和了解,当同时运行多个Luigi工作流时,工作人员的数量是总和。这意味着,如果我同时运行两个工作流,并且在luigi.cfg文件中将worker数设置为n,并且如果工作流同时使用n个以上的worker,则中央计划程序将使用2xn个worker 在Luigi的手册中,我找不到任何方法将工人数量限制为n,即使我同时运行十几个工作流 这是我的luigi.cfg文件Python 在同时运行多个脚本时限制Luigi Worker的数量,python,scheduler,bioinformatics,luigi,Python,Scheduler,Bioinformatics,Luigi,据我所见和了解,当同时运行多个Luigi工作流时,工作人员的数量是总和。这意味着,如果我同时运行两个工作流,并且在luigi.cfg文件中将worker数设置为n,并且如果工作流同时使用n个以上的worker,则中央计划程序将使用2xn个worker 在Luigi的手册中,我找不到任何方法将工人数量限制为n,即使我同时运行十几个工作流 这是我的luigi.cfg文件 [core] workers: 3 这就是我正在使用的示例脚本(它实际上使用了sciluigi(luigi之上的一个层),但我认
[core]
workers: 3
这就是我正在使用的示例脚本(它实际上使用了sciluigi(luigi之上的一个层),但我认为它对任务和调度程序配置没有影响)。我希望当我一起运行它几次时,最后三个工作流会等待前三个工作流完成后再开始
import optparse
import luigi
import sciluigi
import random
import time
import sys
import os
import subprocess
class MyFooWriter(sciluigi.Task):
# We have no inputs here
# Define outputs:
outdir = sciluigi.Parameter();
def out_foo(self):
return sciluigi.TargetInfo(self, os.path.join(self.outdir,'foo.txt'))
def run(self):
with self.out_foo().open('w') as foofile:
foofile.write('foo\n')
class MyFooReplacer(sciluigi.Task):
replacement = sciluigi.Parameter() # Here, we take as a parameter
# what to replace foo with.
outFile = sciluigi.Parameter();
outdir = sciluigi.Parameter();
# Here we have one input, a "foo file":
in_foo = None
# ... and an output, a "bar file":
def out_replaced(self):
return sciluigi.TargetInfo(self, os.path.join(self.outdir, self.outFile))
def run(self):
replacement = ""
with open(self.in_foo().path, 'r') as content_file:
content = content_file.read()
replacement = content.replace('foo', self.replacement)
for i in range(1,30):
sys.stderr.write(str(i)+"\n")
time.sleep(1)
with open(self.out_replaced().path,'w') as out_f:
out_f.write(replacement)
class MyWorkflow(sciluigi.WorkflowTask):
outdir = luigi.Parameter()
def workflow(self):
#rdint = random.randint(1,1000)
rdint = 100
barfile = "foobar_" + str(rdint) +'.bar.txt'
foowriter = self.new_task('foowriter', MyFooWriter, outdir = self.outdir)
fooreplacer = self.new_task('fooreplacer', MyFooReplacer, replacement='bar', outFile = barfile, outdir = self.outdir)
fooreplacer.in_foo = foowriter.out_foo
return fooreplacer
# End of script ....
if __name__ == '__main__':
parser = optparse.OptionParser()
parser.add_option('-d', dest = "outdir", action="store", default=".")
options, remainder = parser.parse_args()
params = {"outdir" : options.outdir}
wf = [MyWorkflow(outdir = options.outdir)]
luigi.build(wf)
这是我用来并发运行脚本的一个小perl脚本(用perl,我最喜欢的语言:-))
#/usr/bin/perl
严格使用;
对于(我的$i=0;$i<6;$i++){
my$testdir=“test”。$i;
系统(“mkdir-p$testdir”);
系统(“python运行_sciluigi.py-d$testdir&”);
睡眠(2)
}
虽然不完全是工作者限制,但可以使用资源
概念对并发执行进行全局限制
在luigi.cfg中
[resources]
max_workers=5
在所有任务中:
class MyFooReplacer(sciluigi.Task):
resources = {'max_workers': 1}
嗨,马特,非常感谢你的回答。这正是我要做的。这个问题主要来自我对工人概念的误解。我或多或少地了解到,工人是管道能够一起运行的任务数量,而资源是工人可以使用的能力,不管他们的数量如何。我还没有找到很多在Luigi任务中使用
资源的例子,所以这太棒了。我用它来解决一个问题,在这个问题上,我希望生成多个写入同一数据库表的任务。
class MyFooReplacer(sciluigi.Task):
resources = {'max_workers': 1}