简体   繁体   中英

How to launch in Ruby commands in parallel

I am writing a terminal program using ruby as a launcher for a set of program written in C++ and Java which should be executed in a distributed system.

I would like to translate this instruction in ruby:

for i in {1..40}; do
  ssh node$i program & #note & so that that process is detached
done

This is my ruby code:

class Launcher
   # Other method that we can ignore
   def order(command)
     @nodelist.each {#Do something here}
   end 
end 

I though about creating a pool of thread and each thread execute that command. Is it the appropriate way? As I studied threads can not execute "exec" since threads share the same memory address.

Here is the solution:

class Launcher

  def initialize(commands_list)
    #Expected to be an array of command string
    @commands_list = commands_list
  end

  def execute
    p_threads = @commands_list.map do |command|
      Process.detach(Process.spawn(command))
    end
    p_threads.each(&:join)
   end
end

Do you know GNU parallel ? It's made for running jobs in parallel (surprise!). It is developped by experienced people, tested and tried. You might just use it instead of reimplementing most of it. Or you might have a look in its manual and/or source code, and maybe learn from it.

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