From: Tony Arcieri Date: 2013-07-19T03:37:12+09:00 Subject: Re: Call a method with block on distributed object (drb) --20cf307f322c61fa5e04e1cd7feb Content-Type: text/plain; charset=ISO-8859-1 Please keep in mind if you pass a block to a remote object, the block will still be executed locally, and the results of the block invocation sent back to the remote object. On Tue, Jul 16, 2013 at 6:35 AM, Enrico Pilotto wrote: > Hi. > I've the follow class for a distributed object: > > > class Counter > def initialize(i = 0) > @index = i > end > def increment > @index += 1 > end > def current > return @index > end > def each(n = 1) > threads = [] > n.times do > threads << Thread.new do > yield(self.current) > end > end > threads.map(&:join) > end > end > > > > Now the server: > > > require 'drb' > require './counter.rb' > > DRb.start_service('druby://127.0.0.1:9000', Counter.new) > puts "Wordlist server running at #{DRb.uri}" > trap('SIGINT') { DRb.stop_service } > DRb.thread.join > > > And the client: > > > require 'drb' > > c = DRbObject.new_with_uri('druby://127.0.0.1:9000') > c.each(8) do |i| > puts i > end > > > I run the server (ok) and then the client and I get the follow error: > > > /Users/pioz/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/drb/drb.rb:1687:in > `current_server': DRb::DRbServerNotFound (DRb::DRbConnError) > from > /Users/pioz/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/drb/drb.rb:1756:in > `to_id' > from > /Users/pioz/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/drb/drb.rb:1071:in > `initialize' > from > /Users/pioz/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/drb/drb.rb:647:in > `new' > from > /Users/pioz/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/drb/drb.rb:647:in > `make_proxy' > from > /Users/pioz/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/drb/drb.rb:564:in > `rescue in dump' > from > /Users/pioz/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/drb/drb.rb:561:in > `dump' > from > /Users/pioz/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/drb/drb.rb:610:in > `send_request' > from > /Users/pioz/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/drb/drb.rb:921:in > `send_request' > from > /Users/pioz/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/drb/drb.rb:1221:in > `send_message' > from > /Users/pioz/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/drb/drb.rb:1110:in > `block (2 levels) in method_missing' > from > /Users/pioz/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/drb/drb.rb:1197:in > `open' > from > /Users/pioz/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/drb/drb.rb:1109:in > `block in method_missing' > from > /Users/pioz/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/drb/drb.rb:1128:in > `with_friend' > from > /Users/pioz/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/drb/drb.rb:1108:in > `method_missing' > from client.rb:5:in `
' > > > Why? > If I change the client in this way: > > > require 'drb' > require 'mechanize' > > c = DRbObject.new_with_uri('druby://127.0.0.1:9000') > threads = [] > 8.times do > threads << Thread.new do > puts c.current > end > end > threads.map(&:join) > > > all works fine. > Any idea or help? > > -- > Posted via http://www.ruby-forum.com/. > > -- Tony Arcieri --20cf307f322c61fa5e04e1cd7feb Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable
Please keep in mind if you pass a block to a remote object= , the block will still be executed locally, and the results of the block in= vocation sent back to the remote object.

On Tue, Jul 16, 2013 at 6:35 AM, Enrico Pilo= tto <lists@ruby-forum.com> wrote:
Hi.
I've the follow class for a distributed object:


=A0 =A0 class Counter
=A0 =A0 =A0 def initialize(i =3D 0)
=A0 =A0 =A0 =A0 @index =3D i
=A0 =A0 =A0 end
=A0 =A0 =A0 def increment
=A0 =A0 =A0 =A0 @index +=3D 1
=A0 =A0 =A0 end
=A0 =A0 =A0 def current
=A0 =A0 =A0 =A0 =A0return @index
=A0 =A0 =A0 end
=A0 =A0 =A0 def each(n =3D 1)
=A0 =A0 =A0 =A0 threads =3D []
=A0 =A0 =A0 =A0 n.times do
=A0 =A0 =A0 =A0 =A0 threads << Thread.new do
=A0 =A0 =A0 =A0 =A0 =A0 yield(self.current)
=A0 =A0 =A0 =A0 =A0 end
=A0 =A0 =A0 =A0 end
=A0 =A0 =A0 =A0 threads.map(&:join)
=A0 =A0 =A0 end
=A0 =A0 end



Now the server:


=A0 =A0 require 'drb'
=A0 =A0 require './counter.rb'

=A0 =A0 DRb.start_service('druby://127.0.0.1:9000', Counter.new)
=A0 =A0 puts "Wordlist server running at #{DRb.uri}"
=A0 =A0 trap('SIGINT') { DRb.stop_service }
=A0 =A0 DRb.thread.join


And the client:


=A0 =A0 require 'drb'

=A0 =A0 c =3D DRbObject.new_with_uri('druby://127.0.0.1:9000')
=A0 =A0 c.each(8) do |i|
=A0 =A0 =A0 puts i
=A0 =A0 end


I run the server (ok) and then the client and I get the follow error:


=A0 =A0 /Users/pioz/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/drb/drb.rb:168= 7:in
`current_server': DRb::DRbServerNotFound (DRb::DRbConnError)
=A0 =A0 =A0 from
/Users/pioz/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/drb/drb.rb:1756:in
`to_id'
=A0 =A0 =A0 from
/Users/pioz/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/drb/drb.rb:1071:in
`initialize'
=A0 =A0 =A0 from
/Users/pioz/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/drb/drb.rb:647:in
`new'
=A0 =A0 =A0 from
/Users/pioz/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/drb/drb.rb:647:in
`make_proxy'
=A0 =A0 =A0 from
/Users/pioz/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/drb/drb.rb:564:in
`rescue in dump'
=A0 =A0 =A0 from
/Users/pioz/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/drb/drb.rb:561:in
`dump'
=A0 =A0 =A0 from
/Users/pioz/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/drb/drb.rb:610:in
`send_request'
=A0 =A0 =A0 from
/Users/pioz/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/drb/drb.rb:921:in
`send_request'
=A0 =A0 =A0 from
/Users/pioz/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/drb/drb.rb:1221:in
`send_message'
=A0 =A0 =A0 from
/Users/pioz/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/drb/drb.rb:1110:in
`block (2 levels) in method_missing'
=A0 =A0 =A0 from
/Users/pioz/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/drb/drb.rb:1197:in
`open'
=A0 =A0 =A0 from
/Users/pioz/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/drb/drb.rb:1109:in
`block in method_missing'
=A0 =A0 =A0 from
/Users/pioz/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/drb/drb.rb:1128:in
`with_friend'
=A0 =A0 =A0 from
/Users/pioz/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/drb/drb.rb:1108:in
`method_missing'
=A0 =A0 =A0 from client.rb:5:in `<main>'


Why?
If I change the client in this way:


=A0 =A0 require 'drb'
=A0 =A0 require 'mechanize'

=A0 =A0 c =3D DRbObject.new_with_uri('druby://127.0.0.1:9000')
=A0 =A0 threads =3D []
=A0 =A0 8.times do
=A0 =A0 =A0 threads << Thread.new do
=A0 =A0 =A0 =A0 puts c.current
=A0 =A0 =A0 end
=A0 =A0 end
=A0 =A0 threads.map(&:join)


all works fine.
Any idea or help?

--
Posted via http://= www.ruby-forum.com/.




-- Tony Arcieri
--20cf307f322c61fa5e04e1cd7feb--