From: "ara.t.howard" Date: 2008-07-30T02:13:53+09:00 Subject: Re: Concurrent Ruby? On Jul 29, 2008, at 10:00 AM, David Masover wrote: > I'm trying to figure out how to at least partly duplicate the semantic > advantage in Ruby, but it's not easy -- I'm stuck either #freeze-ing > everything, or wrapping every message in an actor of its own, and both > approaches seem more obnoxious and error-prone than forcing the > developer to > deal with it. fan out multiple processes with a message queue each - easy to do with drb. naive impl: cfp:~> cat a.rb b got "hello" (pid=94677) a got "hello" (pid=94676) cfp:~> cat a.rb a = actor { recv_msg { |msg| puts "a got #{ msg.inspect } (pid=#{ Process.pid })" } } b = actor { recv_msg { |msg| puts "b got #{ msg.inspect } (pid=#{ Process.pid })" a.send_msg msg } } b.send_msg 'hello' STDIN.gets BEGIN { require 'rubygems' require 'thread' require 'drb' require 'slave' class Actor include ::DRb::DRbUndumped def initialize &block @q = Queue.new @block = block act! end def act! @thread = Thread.new do Thread.current.abort_on_exception = true instance_eval &@block end end def send_msg message @q.push message end def recv_msg while(( message = @q.pop )) yield message end end end def actor(*a, &b) Slave.new{ Actor.new(*a, &b) }.object end STDOUT.sync = true } 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