Ruby - 运行多个进程并分离

时间:2017-09-02 16:36:32

标签: ruby process spawn

我想在macos上从Ruby(2.0.0-p247)运行多个长时间运行的渲染命令。我还需要将数据传递给各个进程的stdin

point_groups.each_with_index do |points, i|
    cmd = "/usr/local/radiance/bin/rtrace -h -n 4 -ab 0 -oMpd -fff /var/folders/gp/r89xmhrj5_ngq8wtx8hthpqr0000gn/T/shadow_mask.oct > /var/folders/gp/r89xmhrj5_ngq8wtx8hthpqr0000gn/T/sm_#{i}.out"
    puts cmd
    stdin, stdout, stderr, wait_thr = Open3.popen3(cmd)
    stdin.puts points.pack("F*")
    stdin.close
    stdout.close
    stderr.close
    puts wait_thr.value
end

这里point_groups是一个浮点数组的数组。每个元素(浮点数组)都被打包并发送到stdin

此方法有效,但流程按顺序执行。

编辑(基于Stefan建议)

threads = []
point_groups.each_with_index do |points, i|
    cmd = "/usr/local/radiance/bin/rtrace -h -n 4 -ab 0 -oMpd -fff /var/folders/gp/r89xmhrj5_ngq8wtx8hthpqr0000gn/T/shadow_mask.oct > /var/folders/gp/r89xmhrj5_ngq8wtx8hthpqr0000gn/T/sm_#{i}.out"
    puts cmd
    stdin, stdout, stderr, wait_thr = Open3.popen3(cmd)
    threads << wait_thr
    stdin.puts points.pack("F*")
    stdin.close
    stdout.close
    stderr.close
end
threads.each{|thr| thr.join }

不幸的是,它的行为方式相同。

0 个答案:

没有答案