From: Robert Klemme Date: 2004-07-13T18:42:22+09:00 Subject: Re: ruby interpreter as mach kernel server (beside bsd) ------=_NextPart_000_0053_01C468CD.ED9F4900 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit "gabriele renzi" schrieb im Newsbeitrag news:j1l5f01kgkff218jr7vnfnbpq4iorbagds@4ax.com... > I think this maps quite good to the actor model in Io > (www.iolanguage.com). > In Io you call a method like: > > obj methodname(args) > > and if you call it like: > obj @methodname(args) > > it returns you a Future object. > You then test the future object and in case you retriev the value: > future_object isReady > future_object value print > > Otherwise you can avoid to handle the future object like this: > obj @@methodname(args) #returns nil > > It is an interesting model, and imho seem very OO, simple and > powerful. To me this approach looks a bit complicate. Normally if you would work completely event base you would just get an event back with the result. Otherwise you could use synchronous calls anyway. > Now if I just was good enough to implement Object#async_send() ... ;) Lemmesee... .... there is a draft attached. Regards robert ------=_NextPart_000_0053_01C468CD.ED9F4900 Content-Type: application/octet-stream; name="async-send.rb" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="async-send.rb" #!/usr/bin/ruby require 'monitor' require 'thread' module AsyncCallable def self.main_queue @main_queue ||=3D create_queue end # might be redefined to use other threading scheme def self.queue_instance; main_queue; end def self.create_queue q =3D Queue.new Thread.new(q) do |queue| while ( item =3D queue.deq ) # p item item.call Thread.pass end end q end class Future < Monitor def result=3D(result) synchronize { @ready =3D true; @result =3D = result } end def error=3D(err) synchronize { @ready =3D true; @error =3D err } = end def ready?; synchronize { @ready } end def result synchronize do raise "Illegal State" unless ready? @result end end def error synchronize do raise "Illegal State" unless ready? @error end end end def initialize; init; end def init @queue ||=3D ( self.class.ancestors.find {|cl| cl.respond_to? = :queue_instance } || AsyncCallable ).queue_instance self end def async_send( sym, *args, &b ) r =3D Future.new @queue.enq lambda { begin r.result=3D( send( sym, *args, &b ) ) rescue Exception =3D> e r.error =3D e end } r end end class AsyncTester include AsyncCallable def test sleep 0.1 "done" end end objects =3D (1..100).map do # str =3D "*" * rand( 100 ) # str.extend AsyncCallable # str.init AsyncTester.new end # p strings futures =3D objects.map do |o| o.async_send :test end # p futures.map {|f| f.ready? } sleep 1 results =3D futures.map do |f| if f.ready? f.error ? f.error : f.result else "not yet" end end p results ------=_NextPart_000_0053_01C468CD.ED9F4900--