简体   繁体   中英

How to execute Rake file tasks in parallel

I'm using Rake to build a C language build system. When compiling multiple files, I want to compile multiple files in parallel, using multiple cores of the CPU.

Can you give me some advice on how to write a Rake file? I would like to achieve the make -j in Rake.

As a restriction, I prefer not to install a new Gem.

For clarity, here is a simplified Rake file.

CC = "gcc"

task :default => "hello"

file "hello" => ["hello.o", "message.o"] do
  sh "#{CC} -o hello hello.o message.o"
end

file "hello.o" => "hello.c" do (1)
  sh "#{CC} -c hello.c"
end

file "message.o" => "message.c" do (2)
  sh "#{CC} -c message.c"
end

For tasks, I can use multitask. However, for file tasks, I don't know how to describe it. I would like to run the file tasks (1) and (2) concurrently.

my environment: ruby 2.6.4p104 (2019-08-28 revision 67798) [i386-cygwin]

I thank you in advance.

You can create threads and you can run your files in new thread.

For example

CC = "gcc"

task :default => "hello"

file "hello" => ["hello.o", "message.o"] do
  Thread.new do
    sh "#{CC} -o hello hello.o message.o"
  end
end

file "hello.o" => "hello.c" do
  Thread.new do
    sh "#{CC} -c hello.c"
  end
end

file "message.o" => "message.c" do
  Thread.new do
    sh "#{CC} -c message.c"
  end
end

I found out that rake -m considers all tasks as multitasking. I defined individual tasks only for the tasks that I do not want to be executed in parallel. By specifying something like "rake -m -j 8", I confirmed that the build time was reduced.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM