IO issues: forking, select, and duplexing

H

Hal Fulton

Suppose I have a source and a sink of data. These are IO-like,
but you can't do a select on them.

I'd like to be able to do a select.

I'd also like to treat these as a single IO object.

So I thought: Well, I'll fork a TCPServer.

I'll let it read from the input stream and write to the client.

I'll let it read from the client and write to the output stream.

Then I'll open a socket to that server, et voila! There is my
select-able duplex IO object.


But in practice, I'm having trouble implementing this.

I won't even show you the code. ;)


Any assistance appreciated.


Hal
 
Y

Yukihiro Matsumoto

Hi,

In message "IO issues: forking, select, and duplexing"

|Suppose I have a source and a sink of data. These are IO-like,
|but you can't do a select on them.
|
|I'd like to be able to do a select.
|
|I'd also like to treat these as a single IO object.

If your IO-like object contains a reference to a real IO object that
can be a target of select, you can call select on the IO-like object
by just adding "to_io" method that returns real IO object.

|So I thought: Well, I'll fork a TCPServer.

If you really need full duplex IO object using fork, how about using
"open" instead of TCPServer:

open("|-", "w+") do
# the code to copy from STDIN to IO-like
# and from IO-like to STDOUT
end

matz.
 
A

Ara.T.Howard

If your IO-like object contains a reference to a real IO object that can be
a target of select, you can call select on the IO-like object by just adding
"to_io" method that returns real IO object.

you mean this will (is) supported?

class Klass
def initialize path
@io = open path
end
def to_io
@io
end
end

cool.

can you give a quick brain dump of other to_XXX method we might want to know
about?

kind regards.

-a
--
===============================================================================
| EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
| PHONE :: 303.497.6469
| A flower falls, even though we love it;
| and a weed grows, even though we do not love it.
| --Dogen
===============================================================================
 
H

Hal Fulton

Yukihiro said:
If your IO-like object contains a reference to a real IO object that
can be a target of select, you can call select on the IO-like object
by just adding "to_io" method that returns real IO object.

That is very interesting. I don't think I can do this easily (as the
code is from a third party), but I'll look at it.
|So I thought: Well, I'll fork a TCPServer.

If you really need full duplex IO object using fork, how about using
"open" instead of TCPServer:

open("|-", "w+") do
# the code to copy from STDIN to IO-like
# and from IO-like to STDOUT
end

This is also very interesting. I will try this soon.


Thanks,
Hal
 
G

Greg Millam

Hal said:
That is very interesting. I don't think I can do this easily (as the
code is from a third party), but I'll look at it.

May I suggest Socket.pair ?

sockets = Socket.pair(Socket::AF_UNIX, Socket::SOCK_STREAM, 0)

sockets[0].puts "hello"
sockets[1].gets #=> "hello\n"

- Greg Millam
 
K

Kent Sibilev

I guess it's already supported. Check out io.c rb_io_get_io() function.

Cheers,
Kent.

If your IO-like object contains a reference to a real IO object that
can be
a target of select, you can call select on the IO-like object by just
adding
"to_io" method that returns real IO object.

you mean this will (is) supported?

class Klass
def initialize path
@io = open path
end
def to_io
@io
end
end

cool.

can you give a quick brain dump of other to_XXX method we might want
to know
about?

kind regards.

-a
--
=======================================================================
========
| EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
| PHONE :: 303.497.6469
| A flower falls, even though we love it;
| and a weed grows, even though we do not love it. | --Dogen
=======================================================================
========
 

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,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top