[英]How to execute Rake file tasks in parallel
我正在使用 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”之類的內容,我確認構建時間減少了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.