如何在Ruby中并行运行文件?

时间:2014-05-04 06:52:46

标签: ruby parallel-processing

如何使用Ruby实现I want to process a bunch of files in parallel, and when one finishes, I want to start the next. And I want to make sure there are exactly 5 jobs running at a time @ Wooledge.org

find . -print0 | xargs -0 -n 1 -P 5 command

find . -print0 | parallel -0 command | use_output_if_needed

for i in *.log ; do
  echo "$i"
  [...do other needed stuff...]
  sem -j10 gzip $i ";" echo done
done
sem --wait

1 个答案:

答案 0 :(得分:3)

使用ruby-pool gem

已经使用了几个月并且喜欢它!

如果您有任何疑问,请与我联系!

示例代码

require 'thread/pool'

pool = Thread.pool(5)

all_files.each do |f|
  pool.process {
    # Do Stuff
  }
end

pool.shutdown