JP. JP. - 1 month ago 16
Ruby Question

Threading in Ruby with a limit

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

Answer

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