如何并行执行 Rake 文件任务

问题描述

我正在使用 Rake 构建一个 C 语言构建系统。 编译多个文件时, 我想使用 CPU 的多个内核并行编译多个文件。

你能给我一些关于如何编写 Rake 文件的建议吗? 我想在 Rake 中实现 make -j

作为限制,我不想安装新的 Gem。

为了清楚起见,这里是一个简化的 Rake 文件。

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

对于任务,我可以使用多任务。 但是,对于文件任务,我不知道如何描述它。 我想同时运行文件任务 (1) 和 (2)。

我的环境: ruby 2.6.4p104 (2019-08-28 修订版 67798) [i386-cygwin]

我先谢谢你。

解决方法

您可以创建线程,并且可以在新线程中运行您的文件。

例如

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
,

我发现 rake -m 将所有任务视为多任务处理。我只为不想并行执行的任务定义了单独的任务。通过指定诸如“rake -m -j 8”之类的内容,我确认构建时间减少了。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...