Making use of multiple processor cores in RubyMost today's computers have more than one processor core, and it's a pity not to make use of that fact, especially when using a language as slow as Ruby. Unfortunately, ruby threads all execute on the same core as the process itself, so no luck there (I think jRuby works better there, but often it's not a choice). But sometimes we can use processes instead of threads. In the following example we're doing just that, forking several processes to execute a task that's suitable for parallel processing.
#!/usr/bin/ruby
# number of simultaneous processes
sim = 4
# array of elements to process
a = (1..20).to_a
# function that processes the data
def do_the_do i
puts "starting #{i}"
# do something, for example, sleep between 5 and 10 seconds
sleep(rand(5)+5)
puts "done #{i}"
end
# starting first N processes
sim.times do
i = a.pop
Process.fork {do_the_do(i)}
end
# start one by one as the previous finish
a.each do |i|
Process.wait(0)
Process.fork {do_the_do(i)}
end
# wait for all to finish
Process.waitall
puts "done all"
The code is explained in the comments, should be pretty straightforward. KomentariAre replies also supposed to be in English? Ok. Sorry for English, I thought that these ruby related posts would be more of interest to non-serbian speaking people... Yes, that's it. The thread/process pool has the advantage that it packs the threading issues (synchronization and checking for processes that terminate) in a black box and you don't need to worry about them once it's in place; thus separating your business logic from process management logic. As the two are unrelated for most of the time, it makes the code easier to understand. Dodajte svoj komentar:© 2003-2009. Mladen Jablanovic
|
||
|
O sajtu Autori FAQ Linkovi KategorijeLičnoOpšte Pretraživači Razvoj Softver Veb Pretraga sajtaArhivapo datumupo kategoriji Powered by pMachine |
||