2

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.

3
  • Do you want your Ruby process to still be alive after it launches everything? Does the Ruby process have to do anything with the output of these commands or wait for them to finish? Commented Feb 22, 2015 at 15:58
  • Hello Andrew, wait them to finish. Commented Feb 22, 2015 at 17:26
  • Each process has at least one thread. As far as I know, all threads of a process are able to do exec. Though exec() replaces one process by another. That means that all the threads that belong to the old process are gone. Commented Mar 1, 2015 at 21:15

2 Answers 2

6
+50

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
Sign up to request clarification or add additional context in comments.

Comments

1

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.

3 Comments

Does it have a ruby library? Or are you refering about launching gnu parallel using the backquotes?
Can you explain how to extend it to a cluster?
@Vicente After looking around a bit, I do not think that you can use GNU parallel as a library. You can call it using backquotes, system or Popen3. I do not have any experience in using it on clusters, but you will want to have a look at stackoverflow.com/questions/22236337/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.