From: Robert Klemme Date: 2004-06-11T02:43:38+09:00 Subject: Re: a __real__ drb question "Ara.T.Howard" schrieb im Newsbeitrag news:Pine.LNX.4.60.0406100854460.10410@harp.ngdc.noaa.gov... > > now that i've already stuck my foot in my mouth, i'd like to ask a real drb > question. > > say you've got a thread accessing an obj which is being served by drb: > > require 'drb/drb' > > mode = ARGV.shift || 'server' > > class Q > def initialize; @q=[]; end > def push obj; @q << obj; end > def pop; @q.shift; end > end > > case mode > when 'server' > q = Q.new > # > # is this safe? > # > Thread.new{loop{ sleep 2; q.push Time.now }} > > DRb.start_service nil, q > uri = DRb.uri > puts "ruby #{ $0 } client #{ DRb.uri }" > DRb.thread.join > > when 'client' > uri = ARGV.shift > DRb.start_service nil, nil > q = DRbObject.new nil, uri > loop do > obj = q.pop > p obj > q.push 42 > sleep 0.5 > end > end > > > my understanding is that access to the object via DRb is provided by multiple > synchronized threads. eg. only one DRb thread would be accessing your object > at once so you generally don't need to consider thread coordination. That was not my understanding and tests seem to back this: > in this > case, however, i have a thread outside of DRb accessing the object - what sort > of coordination would this require? perhaps it would be better to have the > thread obtain a DRbObject itself and allow the DRb library to handle all the > coordination? A general note about concurrency that applies here as well: Typically "automated" synchronization is not done because no automated mechanism can know the granularity of concurrency needed. You might even access an instance concurrently in a proper way without synchronization (e.g. if the instance does not contain any state itself). Another example is a set of operations that has to be done exclusively; a typical example is check for presence and then > > eg. > > ... > when 'server' > q = Q.new > DRb.start_service nil, q > uri = DRb.uri > # > # safe? > # > q = DRbObject.new nil, uri > Thread.new{loop{ sleep 2; q.push Time.now }} > > puts "ruby #{ $0 } client #{ DRb.uri }" > DRb.thread.join > ... > > comments? > > -a > -- > ============================================================================ === > | EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov > | PHONE :: 303.497.6469 > | A flower falls, even though we love it; and a weed grows, even though we do > | not love it. --Dogen > ============================================================================ ===