On Apr 20, 2008, at 12:32 PM, Shot (Piotr Szotkowski) wrote:
> My example code:
>
> results = []
> run = 0
> runs = 2 ** fsm_inputs.size
> (0...runs).forkoff do |vector|
>  results[vector] = by_input_sets vector
>  # snip some run-based stats
>  run += 1
> end
>
> This, obviously, doesnÃÕ work (i.e., results is an empty array at the
> end and run is 0 in every iteration). I can get the results by making
> the block return the by_input_sets callÃÔ result, but I still lose the
> run-based stats.
>
> It seems a singleton-based approach would work (IÃÅ create a singleton
> object outside of the loop and have the results array and run counter
> be its properties), but maybe there is an easier way?

to do this you'll want to combine forkoff with my slave lib: which  
sets up an object which is fronted by drb, an which can indeed be a  
singleton - note that this object is, itself, running in a child  
process, but you can ignore this for the most part.  an simple example:

cfp:~ > cat a.rb
require 'rubygems'
require 'slave'
require 'forkoff'

slave = Slave.new(:threadsafe => true){ Hash.new }
process_global = slave.object

( 0 .. 4 ).each do |i|
   process_global[i] = i ** 2
end

process_global.each do |k,v|
   p k => v
end


cfp:~ > ruby a.rb
{0=>0}
{1=>1}
{2=>4}
{3=>9}
{4=>16}


even with these abstractions you have to consider deeply what's  
happening with threads/processes etc - but yes, it's definitely  
possible with little code.

cheers.

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