Why can't I return "Threads.list" Array in DRb?

  • Thread starter Iñaki Baz Castillo
  • Start date
I

Iñaki Baz Castillo

Hi, I can't understand why I can't return the current "Threads.list" array =
in=20
a DRb server method.

=2D In the DRb server object I just do:

def current_threads
Thread.list
end

=2D And the DRb client calls:

drb_client.current_threads

but instead of an Array object I get a DRb::DRbObject, and in each call to=
=20
this method I get a different "id":

irb> drb_client.current_threads
=3D> #<DRb::DRbObject:0xb7b8d384 @ref=3D-608642478, @uri=3D"druby://0.0.0.0=
:10001">
irb> drb_client.current_threads
=3D> #<DRb::DRbObject:0xb7b8b675 @ref=3D-608644410, @uri=3D"druby://0.0.0.0=
:10001">

Of course, I'm 100% sure that "Thread.list" returns an Array. If I return a=
ny=20
other Array all works ok:

=2D DRb server:

def current_threads
[1,2,3,4,5]
end

irb> drb_client.current_threads
[1,2,3,4,5]


Why does this issue occur when returning "Thread.list"?

Thanks for any help.


=2D-=20
I=C3=B1aki Baz Castillo
 
I

Iñaki Baz Castillo

El Domingo, 29 de Junio de 2008, I=C3=B1aki Baz Castillo escribi=C3=B3:
- In the DRb server object I just do:

def current_threads
Thread.list
end

- And the DRb client calls:

drb_client.current_threads

but instead of an Array object I get a DRb::DRbObject, and in each call to
this method I get a different "id":

irb> drb_client.current_threads
=3D> #<DRb::DRbObject:0xb7b8d384 @ref=3D-608642478,
@uri=3D"druby://0.0.0.0:10001">=20
irb> drb_client.current_threads=20
=3D> #<DRb::DRbObject:0xb7b8b675 @ref=3D-608644410,
@uri=3D"druby://0.0.0.0:10001">

Opsss, and more extrange:
Note the following:

irb> drb_client.current_threads
=3D> #<DRb::DRbObject:0xb7b8d384 @ref=3D-608642478, @uri=3D"druby://0.0.0.0=
:10001">

irb> puts drb_client.current_threads
#<Thread:0xb7c8f700>
#<Thread:0xb76f9598>
#<Thread:0xb76f9a98>
#<Thread:0xb76f9890>
#<Thread:0xb76f91b0>
=3D> nil

How is it possible? why "puts" gets this?


irb> puts drb_client.current_threads.class
DRb::DRbObject
=3D> nil


=2D-=20
I=C3=B1aki Baz Castillo
 
I

Iñaki Baz Castillo

El Domingo, 29 de Junio de 2008, I=C3=B1aki Baz Castillo escribi=C3=B3:
El Domingo, 29 de Junio de 2008, I=C3=B1aki Baz Castillo escribi=C3=B3:

Opsss, and more extrange:
Note the following:

irb> drb_client.current_threads
=3D> #<DRb::DRbObject:0xb7b8d384 @ref=3D-608642478,
@uri=3D"druby://0.0.0.0:10001">

irb> puts drb_client.current_threads
#<Thread:0xb7c8f700>
#<Thread:0xb76f9598>
#<Thread:0xb76f9a98>
#<Thread:0xb76f9890>
#<Thread:0xb76f91b0>
=3D> nil

How is it possible? why "puts" gets this?


irb> puts drb_client.current_threads.class
DRb::DRbObject
=3D> nil

Well, I am understanding that if DRb server return an object unknown for th=
e=20
DRb client, the the only way the client hast to use/show it is as raw value=
,=20
is it?

=2D-=20
I=C3=B1aki Baz Castillo
 
R

Robert Klemme

2008/6/29 I=F1aki Baz Castillo said:
Hi, I can't understand why I can't return the current "Threads.list" arra= y in
a DRb server method.

- In the DRb server object I just do:

def current_threads
Thread.list
end

- And the DRb client calls:

drb_client.current_threads

but instead of an Array object I get a DRb::DRbObject, and in each call t= o
this method I get a different "id":

irb> drb_client.current_threads
=3D> #<DRb::DRbObject:0xb7b8d384 @ref=3D-608642478, @uri=3D"druby://0.0.0= 0:10001">
irb> drb_client.current_threads
=3D> #<DRb::DRbObject:0xb7b8b675 @ref=3D-608644410, @uri=3D"druby://0.0.0= 0:10001">

Of course, I'm 100% sure that "Thread.list" returns an Array. If I return= any
other Array all works ok:

- DRb server:

def current_threads
[1,2,3,4,5]
end

irb> drb_client.current_threads
[1,2,3,4,5]


Why does this issue occur when returning "Thread.list"?

Threads are local to a process so it does not make any sense to
transfer them to another process.

What are you trying to achieve?

Cheers

robert

--=20
use.inject do |as, often| as.you_can - without end
 
L

Lars Christensen

El Domingo, 29 de Junio de 2008, Iñaki Baz Castillo escribió:

Well, I am understanding that if DRb server return an object unknown for the
DRb client, the the only way the client hast to use/show it is as raw value,
is it?

It seems more like a situation where DRb detects that the return
object (the Array) is not fully serializable (contains non-
derializable objects), and hence it returns a "server reference" or
"proxy object" for the object instead. Whenever you invoke method on
the proxy object, they are invoked on the server.

This is really the beauty of DRb: Objects that can't be marshalled /
serialized, become remote objects automatically. You can even 'yield'
to blocks on the client, which is what happens when you "puts" the
array.

Here's a simpler example:

class Thing
include DRb::DRbUndumped # make sure instance are not marshalled and
transferred
def each
puts "each called on server"
3.times { |i| yield(i) }
end
end
class Server
def create_thing
Thing.new
end
end
DRb::start_service("druby://:12000", Server.new)
DRb.thread.join

And a client:

DRb.start_service
remoteServer = DRb::DRbObject.new(nil, "druby://localhost:12000")
remoteThing = remoteServer.create_thing
remoteThing.each { |i| p i }

The 'remoteThing' becomes a DRb::DRbObject which serves as a proxy to
the Thing object on the server. Thing#each is actally run on the
server and yields back to the block on the client.

Lars
 
I

Iñaki Baz Castillo

El Lunes, 30 de Junio de 2008, Robert Klemme escribi=F3:
Threads are local to a process so it does not make any sense to
transfer them to another process.

What are you trying to achieve?

My projects binds for TCP connections (threads) and I want to monitorize th=
em,=20
so I get them via DRb.

Anyway I've changed the data transferred: instead of "Thread.list" (that is=
=20
not serializable) I do something as:
Thread.list.map {|t| {"id"=3D>t.id, "status"=3D>t.status ...... }

I have enough with that :)


=2D-=20
I=F1aki Baz Castillo
 
I

Iñaki Baz Castillo

El Lunes, 30 de Junio de 2008, Lars Christensen escribi=F3:
The 'remoteThing' becomes a DRb::DRbObject which serves as a proxy to
the Thing object on the server. Thing#each is actally run on the
server and yields back to the block on the client.

Really espectacular :)

=2D-=20
I=F1aki Baz Castillo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top