Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parallelism in Ruby

I've got a loop in my Ruby build script that iterates over each project and calls msbuild and does various other bits like minify CSS/JS.

Each loop iteration is independent of the others so I'd like to parallelise it.

How do I do this?

I've tried:

myarray.each{|item|
    Thread.start {

        # do stuff

     }
}
puts "foo"

but Ruby just seems to exit straight away (prints "foo"). That is, it runs over the loop, starts a load of threads, but because there's nothing after the each, Ruby exits killing the other threads :(

I know I can do thread.join, but if I do this inside the loop then it's no longer parallel.

What am I missing?

I'm aware of http://peach.rubyforge.org/ but using that I get all kinds of weird behaviour that look like variable scoping issues that I don't know how to solve.

Edit

It would be useful if I could wait for all child-threads to execute before putting "foo", or at least the main ruby thread exiting. Is this possible?

like image 432
Andrew Bullock Avatar asked Apr 13 '26 06:04

Andrew Bullock


1 Answers

Store all your threads in an array and loop through the array calling join:

threads = myarray.map do |item|
  Thread.start do
    # do stuff
  end
end
threads.each { |thread| thread.join }
puts "foo"
like image 89
Russell Avatar answered Apr 14 '26 20:04

Russell



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!