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

**URL:** <https://www.ruby-forum.com/t/why-cant-i-return-threads-list-array-in-drb/141700>\
**Category:** Ruby\
**Created:** [June 29, 2008, 6:45pm UTC](https://www.ruby-forum.com/t/why-cant-i-return-threads-list-array-in-drb/141700 "2008-06-29T18:45:04Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [June 29, 2008, 6:45pm UTC](https://www.ruby-forum.com/t/why-cant-i-return-threads-list-array-in-drb/141700/1 "2008-06-29T18:45:04Z")

</div>

Hi, I can’t understand why I can’t return the current “Threads.list”  
array in  
a DRb server method.

- In the DRb server object I just do:

- And the DRb client calls:

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  
=\> #\<DRb::DRbObject:0xb7b8d384 @ref=-608642478,  
@uri=“druby://0.0.0.0:10001”\>  
irb\> drb\_client.current\_threads  
=\> #\<DRb::DRbObject:0xb7b8b675 @ref=-608644410,  
@uri=“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:

irb\> drb\_client.current\_threads  
[1,2,3,4,5]

Why does this issue occur when returning “Thread.list”?

Thanks for any help.

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [June 29, 2008, 6:56pm UTC](https://www.ruby-forum.com/t/why-cant-i-return-threads-list-array-in-drb/141700/2 "2008-06-29T18:56:48Z")

</div>

El Domingo, 29 de Junio de 2008, IÃ±aki Baz C. escribiÃ³:

> 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  
> =\> #\<DRb::DRbObject:0xb7b8d384 @ref=-608642478,  
> @uri=“druby://0.0.0.0:10001”\>  
> irb\> drb\_client.current\_threads  
> =\> #\<DRb::DRbObject:0xb7b8b675 @ref=-608644410,  
> @uri=“druby://0.0.0.0:10001”\>

Opsss, and more extrange:  
Note the following:

irb\> drb\_client.current\_threads  
=\> #\<DRb::DRbObject:0xb7b8d384 @ref=-608642478,  
@uri=“druby://0.0.0.0:10001”\>

irb\> puts drb\_client.current\_threads  
#Thread:0xb7c8f700  
#Thread:0xb76f9598  
#Thread:0xb76f9a98  
#Thread:0xb76f9890  
#Thread:0xb76f91b0  
=\> nil

How is it possible? why “puts” gets this?

irb\> puts drb\_client.current\_threads.class  
DRb::DRbObject  
=\> nil

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [June 30, 2008, 1:10pm UTC](https://www.ruby-forum.com/t/why-cant-i-return-threads-list-array-in-drb/141700/3 "2008-06-30T13:10:50Z")

</div>

2008/6/29 Iñaki Baz C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Of course, I’m 100% sure that “Thread.list” returns an Array. If I return any
> 
> 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

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [June 30, 2008, 2:50pm UTC](https://www.ruby-forum.com/t/why-cant-i-return-threads-list-array-in-drb/141700/4 "2008-06-30T14:50:05Z")

</div>

On Jun 29, 7:00 pm, Iñaki Baz C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> El Domingo, 29 de Junio de 2008, Iñaki Baz C. escribió:
> 
> > El Domingo, 29 de Junio de 2008, Iñaki Baz C. escribió:
> > 
> > > but instead of an Array object I get a DRb::DRbObject, and in each call  
> > > to this method I get a different “id”:
> 
> 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

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [June 29, 2008, 7:04pm UTC](https://www.ruby-forum.com/t/why-cant-i-return-threads-list-array-in-drb/141700/5 "2008-06-29T19:04:25Z")

</div>

El Domingo, 29 de Junio de 2008, IÃ±aki Baz C. escribiÃ³:

> > 
> 
> Opsss, and more extrange:  
> #Thread:0xb76f9890  
> #Thread:0xb76f91b0  
> =\> nil
> 
> How is it possible? why “puts” gets this?
> 
> irb\> puts drb\_client.current\_threads.class  
> DRb::DRbObject  
> =\> nil

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?

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [June 30, 2008, 8:09pm UTC](https://www.ruby-forum.com/t/why-cant-i-return-threads-list-array-in-drb/141700/6 "2008-06-30T20:09:15Z")

</div>

El Lunes, 30 de Junio de 2008, Robert K.  
escribió:\> 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  
them,  
so I get them via DRb.

Anyway I’ve changed the data transferred: instead of “Thread.list” (that  
is  
not serializable) I do something as:  
Thread.list.map {|t| {“id”=\>t.id, “status”=\>t.status … }

I have enough with that 🙂

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [June 30, 2008, 8:10pm UTC](https://www.ruby-forum.com/t/why-cant-i-return-threads-list-array-in-drb/141700/7 "2008-06-30T20:10:25Z")

</div>

El Lunes, 30 de Junio de 2008, Lars C.  
escribió:\> 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 🙂
