Call a method with block on distributed object (drb)

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 `<main>'

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/.

Hi.

...

     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)

Insert this line at the beginning of your client:

DRb.start_service

The server is trying to yield to code on the client. This is the same as sending #call to a remote proc in the client. As with any remote method call, there has to be a listening drb server in the client.

···

On 07/16/2013 06:35 AM, Enrico Pilotto wrote:

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 <lists@ruby-forum.com>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 `<main>'

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