views:

102

answers:

3

I have a task I need to perform, do_stuff(opts), that will take ~1s each, even while 1 - 10 of them are running in parallel. I need to collect an array of the results for each operation at the end.

If I have 30 stuffs to do, how would I use threading effectively to queue up the do_stuff(opts) operations so no more than 10 are running concurrently, but the array of results is not given/printed/etc until all (30) tasks have been completed?

I usually have at least some code to try and illustrate what I mean, but with threading I'm at a bit of a loss! Thanks in advance

+2  A: 

You need to implement this pattern
This question discusses how that can be done in Ruby

adi92
A: 

Also, take a look at this tutorial if you are new to Ruby threads.

jkndrkn
+1  A: 

If you're really after performance, you might also want to look into jruby.
It uses actual OS threads and not the green threads the other ruby implementations use

Marc Seeger