[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Thread IO

Martin Ceronio

2/26/2009 10:35:00 AM

What can I use as a general purpose duplexed IO object to communicate
via data streams between two threads? I would like something similar to
TCPSocket, but without the need to communicate via a network port.
--
Posted via http://www.ruby-....

11 Answers

Martin Ceronio

2/26/2009 10:51:00 AM

0

Martin Ceronio wrote:
> What can I use as a general purpose duplexed IO object to communicate
> via data streams between two threads? I would like something similar to
> TCPSocket, but without the need to communicate via a network port.

I've thought of creating two pipes (because I understand by nature that
one end is read-only and the other write-only) and then giving each
thread a read end and a write end, as shown below. The problem is that
this hangs indefinitely, perhaps just highlighting my lack of
understanding of how IO works (if that is the case, please be so kind as
to put me out of my misery and point me to a good primer).

So, here goes:

gui_rd, ctr_wr = IO.pipe
ctr_rd, gui_wr = IO.pipe

gui = Thread.new(gui_rd, gui_wr) {|gui_rd, gui_wr|
gui_wr << "I've got something for you\n"
puts gui_rd.gets
}

ctr = Thread.new(ctr_rd, ctr_wr) {|ctr_rd, ctr_wr|
puts ctr_rd.gets
ctr_wr << "Right back at you\n"
}

gui.join
ctr.join
--
Posted via http://www.ruby-....

Dylan Evans

2/26/2009 11:47:00 AM

0

[Note: parts of this message were removed to make it a legal post.]

That's a pretty standard way to do IPC and is generally a good idea. Also
your code works for me. What ruby implementation are you using?


On Thu, Feb 26, 2009 at 8:50 PM, Martin Ceronio
<martin.ceronio@absa.co.za>wrote:

> Martin Ceronio wrote:
> > What can I use as a general purpose duplexed IO object to communicate
> > via data streams between two threads? I would like something similar to
> > TCPSocket, but without the need to communicate via a network port.
>
> I've thought of creating two pipes (because I understand by nature that
> one end is read-only and the other write-only) and then giving each
> thread a read end and a write end, as shown below. The problem is that
> this hangs indefinitely, perhaps just highlighting my lack of
> understanding of how IO works (if that is the case, please be so kind as
> to put me out of my misery and point me to a good primer).
>
> So, here goes:
>
> gui_rd, ctr_wr = IO.pipe
> ctr_rd, gui_wr = IO.pipe
>
> gui = Thread.new(gui_rd, gui_wr) {|gui_rd, gui_wr|
> gui_wr << "I've got something for you\n"
> puts gui_rd.gets
> }
>
> ctr = Thread.new(ctr_rd, ctr_wr) {|ctr_rd, ctr_wr|
> puts ctr_rd.gets
> ctr_wr << "Right back at you\n"
> }
>
> gui.join
> ctr.join
> --
> Posted via http://www.ruby-....
>
>


--
The UNIX system has a command, nice ... in order to be nice to the other
users. Nobody ever uses it." - Andrew S. Tanenbaum

Martin Ceronio

2/26/2009 12:03:00 PM

0

I am running on
ruby 1.8.6 (2007-09-24 patchlevel 111) [i386-mswin32]
(from the one-click installer)
--
Posted via http://www.ruby-....

Martin Ceronio

2/26/2009 12:13:00 PM

0

I have just tried it on

ruby 1.8.7 (2008-08-11 patchlevel 72) [i486-linux]

and there it works!

If this is a Windows issue, is there possibly a workaround?
--
Posted via http://www.ruby-....

Martin Ceronio

2/26/2009 12:18:00 PM

0

Martin Ceronio wrote:
> I am running on
> ruby 1.8.6 (2007-09-24 patchlevel 111) [i386-mswin32]
> (from the one-click installer)

Oh yes, and I'm running on Windows XP SP2 if that makes a difference.
--
Posted via http://www.ruby-....

Martin Ceronio

2/26/2009 12:47:00 PM

0

The other thing to note is that this does not happen with TCPSocket
(again on Windows in my case), because the following works for me:

require 'socket'
require 'gserver'

class MyServer < GServer
def initialize(port=9876, *args)
super(port, *args)
end
def serve(io)
puts io.gets
stop
end
end

server = MyServer.new.start
TCPSocket.new("localhost", 9876) << "I have found you!\n"
server.join

So other than the pipes and the sockets, is there another IO object I
can use to communicate between two threads?
--
Posted via http://www.ruby-....

Robert Klemme

2/26/2009 1:03:00 PM

0

2009/2/26 Martin Ceronio <martin.ceronio@absa.co.za>:
> What can I use as a general purpose duplexed IO object to communicate
> via data streams between two threads? I would like something similar to
> TCPSocket, but without the need to communicate via a network port.

You can use class Queue.

Kind regards

robert

--
remember.guy do |as, often| as.you_can - without end

Brian Candler

2/26/2009 1:13:00 PM

0

Martin Ceronio wrote:
> What can I use as a general purpose duplexed IO object to communicate
> via data streams between two threads? I would like something similar to
> TCPSocket, but without the need to communicate via a network port.

Socket.pair is the low-level way of doing this (equivalent to
socketpair())

But if it's just between two Ruby threads, I'd use Queue or SizedQueue.
See thread.rb in the standard library.
--
Posted via http://www.ruby-....

Martin Ceronio

2/26/2009 1:22:00 PM

0

Thanks very much Robert and Brian.

This might be what I am looking for. I will do some investigation to see
if I can use this instead.

Now, it would still be interesting to know why the pipes worked on Linux
but not on Windows.
--
Posted via http://www.ruby-....

Dylan Evans

2/27/2009 3:27:00 AM

0

[Note: parts of this message were removed to make it a legal post.]

On Thu, Feb 26, 2009 at 11:22 PM, Martin Ceronio
<martin.ceronio@absa.co.za>wrote:

> Thanks very much Robert and Brian.
>
> This might be what I am looking for. I will do some investigation to see
> if I can use this instead.
>
> Now, it would still be interesting to know why the pipes worked on Linux
> but not on Windows.

Good question. Might be a bug. Not sure how pipes are implemented in windows
but in linux they're native. Maybe it would work in jruby.




--
The UNIX system has a command, nice ... in order to be nice to the other
users. Nobody ever uses it." - Andrew S. Tanenbaum