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

时间:2022-08-05 21:37:14

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?

我如何实现我想要并行处理一堆文件,当一个文件完成时,我想开始下一个。我想确保在@ Wooledge.org上使用Ruby正好运行5个作业?

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.

等等

1 个解决方案

#1


3  

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

#1


3  

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