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
Only one thread may load or unload at a time, and to do either, it must wait until no other threads are running application code.
Thread Limits Because each thread consumes part of a process's address space, processes have a basic limit on the number of threads they can create that's imposed by the size of their address space divided by the thread stack size.
Multi-threading is the most useful property of Ruby which allows concurrent programming of two or more parts of the program for maximizing the utilization of CPU. Each part of a program is called Thread. So, in other words, threads are lightweight processes within a process.
The Ruby Interpreter is single threaded, which is to say that several of its methods are not thread safe.
I dunno how well it'll work for a more complex application, but I found something like this to work nicely for a simple threading scenario with macruby.
thread_limit = 4
threads = []
things_to_process.each do |thing|
until threads.map { |t| t.status }.count("run") < thread_limit do sleep 5 end
threads << Thread.new { the_task(thing) }
end
output = threads.map { |t| t.value }
the until loop waits around until there are less than the specified number of created threads running before allowing execution of the main thread to continue on to start the next thread.
the output variable will be assigned an array of the values returned by the_task with an ordering corresponding to the input array things_to_process
. The main thread will block until every created thread returns a value.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With