Rake创建一个使用多任务的规则

时间:2022-07-31 13:53:25

If I create a simple rule like

如果我创建一个简单的规则。

rule '.o' => ['.c'] do |t|
  sh "cc #{t.source} -c -o #{t.name}"
end

How can I tell Rake that I want the auto-generated tasks to be parallelizable?

如何才能告诉Rake我希望自动生成的任务是可并行的?

3 个解决方案

#1


5  

as stated by Dennis Rake rules are implemented as Tasks

如Dennis Rake所述,规则被实现为任务

therefore the -m flag which converts tasks to multitasks also converts rules to 'multirules'

因此,将任务转换为多任务的-m标志也将规则转换为“多规则”

require 'rake/clean'

rule '.ext2' => '.ext1' do |t|
    sh "cp #{t.source} #{t.name}"
    sleep(1)
end

def dependencies(input_file)
    base, is, ext = input_file.split('.')
    _from, _to = is.split('-')
    files = []
    Integer(_from).upto(Integer(_to)) do |i|
        files << "#{base}_#{i}.#{ext}"
    end
    return files
end

rule ".ext2" => lambda { |i| dependencies(i) } do |t|
    sh "touch #{t.source}"
end

task :make_files do |t|
    1.upto(20) do |i| 
        sh "touch file_#{i}.ext1"
    end
end

CLEAN = FileList['*.ext2']

running the following commands (not threaded):

运行以下命令(非线程):

rake make_files
time rake file.1-20.ext2

I get

我得到

real    0m20.232s
user    0m0.134s
sys     0m0.082s

with 5 threads:

与5个线程:

rake clean
time rake -m -j 5 file.1-20.ext2

I get

我得到

real    0m4.152s
user    0m0.122s
sys     0m0.071s

with 20 threads:

有20个线程:

rake clean
time rake -m -j 20 file.1-20.ext2

I get

我得到

real    0m1.167s
user    0m0.130s
sys     0m0.065s

I use sleep(1) to emulate 'work/io', if your processes block a lot or if you have many cores this might be useful to you :)

我使用sleep(1)来模拟“work/io”,如果您的进程阻塞了很多,或者如果您有很多内核,这可能对您很有用:)

EDIT: As pointed out by Shadow in comments, the follow toggles 'multitask' to be always on

编辑:正如影子在评论中指出的那样,跟踪“多任务”始终在进行。

Rake.application.options.always_multitask = true

#2


1  

The question, isn't very clear, but isn't Rake::MultiTask what you're looking for?

问题,不是很清楚,但不是Rake::多任务你在找什么?

http://rake.rubyforge.org/Rake/MultiTask.html

http://rake.rubyforge.org/Rake/MultiTask.html

I've neverused it myself, but the docs suggest:

我自己从未用过,但医生建议:

multitask :copy_files => [:copy_src, :copy_doc, :copy_bin] do
  puts "All Copies Complete"
end

http://rake.rubyforge.org/files/doc/rakefile_rdoc.html

http://rake.rubyforge.org/files/doc/rakefile_rdoc.html

The definition of rule in the DSL is in lib/rake/dsl_definitions.rb (in Rake 10.1.1):

DSL中的规则定义在lib/rake/dsl_definition中。rb(Rake大家):

# Declare a rule for auto-tasks.
#
# Example:
#  rule '.o' => '.c' do |t|
#    sh %{cc -o #{t.name} #{t.source}}
#  end
#
def rule(*args, &block)
  Rake::Task.create_rule(*args, &block)
end

A cursory scan of the current code yields nothing built-in for the rule construct in the DSL, though. In case it helps, searching the code for things like @rules and rules. yields a single interesting call in lib/rake/task_manager.rb in the form of enhance_with_matching_rule().

不过,粗略浏览一下当前的代码,就不会对DSL中的规则构造产生任何内置的功能。如果有帮助,可以搜索代码,比如@rules和rules。在lib/rake/task_manager中生成一个有趣的调用。rb的形式是enhanced ce_with_matching_rule()。

#3


0  

I've struggled with this for a bit. The problem is that rule doesn't allow for parallel execution of its dependencies, while multitask allows for neither regex- or glob-based matching, nor for lambdas as dependencies. The latter just gives you Don't know how to build task '#<Proc:0x007fd95a4ce3f8@/Rakefile:14 (lambda)>' (see --tasks).

我为此挣扎了一会儿。问题是规则不允许并行执行其依赖项,而multitask既不允许基于regex或全局的匹配,也不允许将lambdas作为依赖项。后者只会让您不知道如何构建task '# <0x007fd95a4ce3f8 rakefile:14="">

>

#1


5  

as stated by Dennis Rake rules are implemented as Tasks

如Dennis Rake所述,规则被实现为任务

therefore the -m flag which converts tasks to multitasks also converts rules to 'multirules'

因此,将任务转换为多任务的-m标志也将规则转换为“多规则”

require 'rake/clean'

rule '.ext2' => '.ext1' do |t|
    sh "cp #{t.source} #{t.name}"
    sleep(1)
end

def dependencies(input_file)
    base, is, ext = input_file.split('.')
    _from, _to = is.split('-')
    files = []
    Integer(_from).upto(Integer(_to)) do |i|
        files << "#{base}_#{i}.#{ext}"
    end
    return files
end

rule ".ext2" => lambda { |i| dependencies(i) } do |t|
    sh "touch #{t.source}"
end

task :make_files do |t|
    1.upto(20) do |i| 
        sh "touch file_#{i}.ext1"
    end
end

CLEAN = FileList['*.ext2']

running the following commands (not threaded):

运行以下命令(非线程):

rake make_files
time rake file.1-20.ext2

I get

我得到

real    0m20.232s
user    0m0.134s
sys     0m0.082s

with 5 threads:

与5个线程:

rake clean
time rake -m -j 5 file.1-20.ext2

I get

我得到

real    0m4.152s
user    0m0.122s
sys     0m0.071s

with 20 threads:

有20个线程:

rake clean
time rake -m -j 20 file.1-20.ext2

I get

我得到

real    0m1.167s
user    0m0.130s
sys     0m0.065s

I use sleep(1) to emulate 'work/io', if your processes block a lot or if you have many cores this might be useful to you :)

我使用sleep(1)来模拟“work/io”,如果您的进程阻塞了很多,或者如果您有很多内核,这可能对您很有用:)

EDIT: As pointed out by Shadow in comments, the follow toggles 'multitask' to be always on

编辑:正如影子在评论中指出的那样,跟踪“多任务”始终在进行。

Rake.application.options.always_multitask = true

#2


1  

The question, isn't very clear, but isn't Rake::MultiTask what you're looking for?

问题,不是很清楚,但不是Rake::多任务你在找什么?

http://rake.rubyforge.org/Rake/MultiTask.html

http://rake.rubyforge.org/Rake/MultiTask.html

I've neverused it myself, but the docs suggest:

我自己从未用过,但医生建议:

multitask :copy_files => [:copy_src, :copy_doc, :copy_bin] do
  puts "All Copies Complete"
end

http://rake.rubyforge.org/files/doc/rakefile_rdoc.html

http://rake.rubyforge.org/files/doc/rakefile_rdoc.html

The definition of rule in the DSL is in lib/rake/dsl_definitions.rb (in Rake 10.1.1):

DSL中的规则定义在lib/rake/dsl_definition中。rb(Rake大家):

# Declare a rule for auto-tasks.
#
# Example:
#  rule '.o' => '.c' do |t|
#    sh %{cc -o #{t.name} #{t.source}}
#  end
#
def rule(*args, &block)
  Rake::Task.create_rule(*args, &block)
end

A cursory scan of the current code yields nothing built-in for the rule construct in the DSL, though. In case it helps, searching the code for things like @rules and rules. yields a single interesting call in lib/rake/task_manager.rb in the form of enhance_with_matching_rule().

不过,粗略浏览一下当前的代码,就不会对DSL中的规则构造产生任何内置的功能。如果有帮助,可以搜索代码,比如@rules和rules。在lib/rake/task_manager中生成一个有趣的调用。rb的形式是enhanced ce_with_matching_rule()。

#3


0  

I've struggled with this for a bit. The problem is that rule doesn't allow for parallel execution of its dependencies, while multitask allows for neither regex- or glob-based matching, nor for lambdas as dependencies. The latter just gives you Don't know how to build task '#<Proc:0x007fd95a4ce3f8@/Rakefile:14 (lambda)>' (see --tasks).

我为此挣扎了一会儿。问题是规则不允许并行执行其依赖项,而multitask既不允许基于regex或全局的匹配,也不允许将lambdas作为依赖项。后者只会让您不知道如何构建task '# <0x007fd95a4ce3f8 rakefile:14="">

>