Question

How do I achieve 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 using Ruby?

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

or

find . -print0 | parallel -0 command | use_output_if_needed

or

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

etc.

Was it helpful?

Solution

Use the ruby-pool gem.

Been using it for a couple of months and love it!

Let me know if you have any questions!

Example Code

require 'thread/pool'

pool = Thread.pool(5)

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

pool.shutdown
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top