NAME

   forkoff

SYNOPSIS

   brain-dead simple parallel processing for ruby

URI

   http://rubyforge.org/projects/codeforpeople

INSTALL

   gem install forkoff

DESCRIPTION

   forkoff works for any enumerable object, iterating a code block to  
run in a
   child process and collecting the results.  forkoff can limit the  
number of
   child processes which is, by default, 8.

SAMPLES

   <========< samples/a.rb >========>

   ~ > cat samples/a.rb

     #
     # forkoff makes it trivial to do parallel processing with ruby,  
the following
     # prints out each word in a separate process
     #

       require 'forkoff'

       %w( hey you ).forkoff!{|word| puts "#{ word } from  
#{ Process.pid }"}

   ~ > ruby samples/a.rb

     hey from 3239
     you from 3240


   <========< samples/b.rb >========>

   ~ > cat samples/b.rb

     #
     # for example, this takes only 1 second or so to complete
     #

       require 'forkoff'

       a = Time.now.to_f

       results =
         (0..7).forkoff do |i|

           sleep 1

           i ** 2

         end

       b = Time.now.to_f

       elapsed = b - a

       puts "elapsed: #{ elapsed }"
       puts "results: #{ results.inspect }"

   ~ > ruby samples/b.rb

     elapsed: 1.07044386863708
     results: [0, 1, 4, 9, 16, 25, 36, 49]


   <========< samples/c.rb >========>

   ~ > cat samples/c.rb

     #
     # forkoff does *NOT* spawn processes in batches, waiting for each  
batch to
     # complete.  rather, it keeps a certain number of processes busy  
until all
     # results have been gathered.  in otherwords the following will  
ensure that 2
     # processes are running at all times, until the list is complete.  
note that
     # the following will take about 2 seconds to run (2 sets of 2 @ 1  
second).
     #

     require 'forkoff'

     pid = Process.pid

     a = Time.now.to_f

     pstrees =
       %w( a b c d ).forkoff! :processes => 2 do |letter|
         sleep 1
         { letter => ` pstree -l 2 #{ pid } ` }
       end


     b = Time.now.to_f

     puts
     puts "pid: #{ pid }"
     puts "elapsed: #{ b - a }"
     puts

     require 'yaml'

     pstrees.each do |pstree|
       y pstree
     end

   ~ > ruby samples/c.rb


     pid: 3254
     elapsed: 2.12998485565186

     ---
     a: |
       -+- 03254 ahoward ruby -Ilib samples/c.rb
        |-+- 03255 ahoward ruby -Ilib samples/c.rb
        \-+- 03256 ahoward ruby -Ilib samples/c.rb

     ---
     b: |
       -+- 03254 ahoward ruby -Ilib samples/c.rb
        |-+- 03255 ahoward ruby -Ilib samples/c.rb
        \-+- 03256 ahoward ruby -Ilib samples/c.rb

     ---
     c: |
       -+- 03254 ahoward ruby -Ilib samples/c.rb
        |-+- 03261 ahoward (ruby)
        \-+- 03262 ahoward ruby -Ilib samples/c.rb

     ---
     d: |
       -+- 03254 ahoward ruby -Ilib samples/c.rb
        |-+- 03261 ahoward ruby -Ilib samples/c.rb
        \-+- 03262 ahoward ruby -Ilib samples/c.rb





a @ http://codeforpeople.com/
--
we can deny everything, except that we have the possibility of being  
better. simply reflect on that.
h.h. the 14th dalai lama