简体   繁体   中英

How to run files in parallel in Ruby?

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM