On May 27, 2006, at 11:14 AM, ara.t.howard / noaa.gov wrote:

> On Sat, 27 May 2006, ReggW wrote:
>
>> Francis Cianfrocca wrote:
>>
>>> It seems to me that Ruby's green-thread implementation is perfectly
>>> adequate
>>> for most programmers' requirements.
>>
>> But the problem is that it doesn't take advantage of these new  
>> multi-core
>> processor that are now starting to become the standard machines  
>> being sold
>> (at least for my customers).
>
> it's a small problem.  here is some code which starts two  
> processes, three if
> you count the parent.  both run in separate processes using drb as  
> the ipc
> layer to make the communication painless.  because the code uses  
> drb the com is
> simple.  because it uses multiple processes it allows the kernel to  
> migrate
> them to different cpus.  the cost is about 100 lines of pure-ruby  
> (the slave
> lib).  notice how easy it is for parent to communicate with child  
> and for
> childrent to communicate with each other:
>
>     harp:~ > cat a.rb
>     require 'slave'
>     require 'yaml'
>
>     class ProcessA
>       def initialize(b) @b = b end
>       def process(n) @b.process(n * n) end
>       def pid() Process.pid end
>     end
>
>     class ProcessB
>       def process(n) n + 6 end
>       def pid() Process.pid end
>     end
>
>     b = Slave.new(ProcessB.new).object
>     a = Slave.new(ProcessA.new(b)).object
>
>     y 'a.pid' => a.pid
>     y 'b.pid' => b.pid
>
>     y 'answer' => a.process(6)
>
>
>     harp:~ > ruby a.rb
>     ---
>     a.pid: 15142
>     ---
>     b.pid: 15141
>     ---
>     answer: 42
>
>
> this is one of those things that allows one to consider designs  
> that would be
> untenable in other languages.  obviously using this approach it  
> would be
> trivial to setup a job that spawned 16 intercommunicating proccess,  
> something
> which would be absurd to code in c.
>
> regards.
>
>
> -a
> -- 
> be kind whenever possible... it is always possible.
> - h.h. the 14th dali lama
>

Dammit! I was about to write this library!

(Mine was going to look a little different:
   require 'task'


   x = "Hello"
   Task.new(x) { |o| puts o.upcase } # sets up Drb to have a connection
                                     # to x, forks, connects to x via  
drb and
                                     # and yields the DrbObject to  
the block

)