代码之家  ›  专栏  ›  技术社区  ›  Vicente Bolea

如何在Ruby命令中并行启动

  •  2
  • Vicente Bolea  · 技术社区  · 11 年前

    我正在编写一个终端程序,使用ruby作为一组用C++和Java编写的程序的启动器,这些程序应该在分布式系统中执行。

    我想用ruby翻译此指令:

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

    这是我的红宝石代码:

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

    我想创建一个线程池,每个线程执行该命令。这是合适的方式吗?正如我所研究的,线程不能执行“exec”,因为线程共享相同的内存地址。

    2 回复  |  直到 11 年前
        1
  •  6
  •   intale    11 年前

    解决方案如下:

    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
    
        2
  •  1
  •   hagello    11 年前

    你知道吗 GNU parallel ? 它适合并行运行作业(令人惊讶!)。它是由经验丰富的人开发、测试和试用的。你可以直接使用它,而不是重新实现它的大部分。或者你可以看看它的手册和/或源代码,然后从中学习。