IO issues: forking, select, and duplexing

163 views
Skip to first unread message

Hal Fulton

unread,
Jul 29, 2004, 4:44:40 AM7/29/04
to
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

Yukihiro Matsumoto

unread,
Jul 29, 2004, 5:01:22 AM7/29/04
to
Hi,

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


on 04/07/29, Hal Fulton <hal...@hypermetrics.com> writes:

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


Ara.T.Howard

unread,
Jul 29, 2004, 9:21:59 AM7/29/04
to
On Thu, 29 Jul 2004, Yukihiro Matsumoto wrote:

> 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
===============================================================================

Hal Fulton

unread,
Jul 29, 2004, 9:46:05 AM7/29/04
to
Yukihiro Matsumoto wrote:
>
> 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


Greg Millam

unread,
Jul 29, 2004, 10:07:53 AM7/29/04
to
Hal Fulton wrote:
>> 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

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


Kent Sibilev

unread,
Jul 29, 2004, 10:55:58 AM7/29/04
to
I guess it's already supported. Check out io.c rb_io_get_io() function.

Cheers,
Kent.

Reply all
Reply to author
Forward
0 new messages