Vic*_*lea 2 ruby linux multithreading exec
我正在编写一个终端程序,使用ruby作为一个用C++和Java编写的程序的启动器,它应该在分布式系统中执行.
我想在ruby中翻译这条指令:
for i in {1..40}; do
ssh node$i program & #note & so that that process is detached
done
Run Code Online (Sandbox Code Playgroud)
这是我的红宝石代码:
class Launcher
# Other method that we can ignore
def order(command)
@nodelist.each {#Do something here}
end
end
Run Code Online (Sandbox Code Playgroud)
我想创建一个线程池,每个线程执行该命令.这是合适的方式吗?因为我研究线程不能执行"exec",因为线程共享相同的内存地址.
这是解决方案:
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
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
691 次 |
最近记录: |