Select()

Hi Everyone,

I’m very new to Ruby, I generally work in Python. I am wanting to try writing a simple little chat server in it. The problem is, I’m trying to find something in ruby similar to the select() function in C/Python that returns a list of readable/writeable/error sockets, but I can’t seem to find one.

Am I being completely dense and missing something obvious? I’ll be the first to admit I’m not a very good programmer, so thats entirely possible. I would appreciate it if someone could point me to a reference to it, or even a complete chat server program.

Thanks,
Kuros

I'm very new to Ruby, I generally work in Python. I am wanting to try =
writing a simple little chat server in it. The problem is, I'm trying to =
find something in ruby similar to the select() function in C/Python that =
returns a list of readable/writeable/error sockets, but I can't seem to =
find one.

Kernel::select

pigeon% ri Kernel::select
--------------------------------------------------------- Kernel::select
     select( readArray [, writeArray [errorArray [timeout]]] ) ->
     anArray or nil

···

------------------------------------------------------------------------
     Performs a low-level select call, which waits for data to become
     available from input/output devices. The first three parameters are
     arrays of IO objects or nil. The last is a timeout in seconds,
     which should be an Integer or a Float. The call waits for data to
     become available for any of the IO objects in readArray, for
     buffers to have cleared sufficiently to enable writing to any of
     the devices in writeArray, or for an error to occur on the devices
     in errorArray. If one or more of these conditions are met, the call
     returns a three-element array containing arrays of the IO objects
     that were ready. Otherwise, if there is no change in status for
     timeout seconds, the call returns nil. If all parameters are nil,
     the current thread sleeps forever.
        select( [$stdin], nil, nil, 1.5 ) #=> [[#<IO:0x4019202c>], , ]

pigeon%

Guy Decoux

Another option might be to use threads.

robert

“ts” decoux@moulon.inra.fr schrieb im Newsbeitrag
news:200304231016.h3NAGcG08797@moulon.inra.fr

···

I’m very new to Ruby, I generally work in Python. I am wanting to try
=
writing a simple little chat server in it. The problem is, I’m trying
to =
find something in ruby similar to the select() function in C/Python
that =
returns a list of readable/writeable/error sockets, but I can’t seem
to =
find one.

Kernel::select

pigeon% ri Kernel::select
--------------------------------------------------------- Kernel::select
select( readArray [, writeArray [errorArray [timeout]]] ) →
anArray or nil

 Performs a low-level select call, which waits for data to become
 available from input/output devices. The first three parameters are
 arrays of IO objects or nil. The last is a timeout in seconds,
 which should be an Integer or a Float. The call waits for data to
 become available for any of the IO objects in readArray, for
 buffers to have cleared sufficiently to enable writing to any of
 the devices in writeArray, or for an error to occur on the devices
 in errorArray. If one or more of these conditions are met, the call
 returns a three-element array containing arrays of the IO objects
 that were ready. Otherwise, if there is no change in status for
 timeout seconds, the call returns nil. If all parameters are nil,
 the current thread sleeps forever.
    select( [$stdin], nil, nil, 1.5 )   #=> [[#<IO:0x4019202c>], [],

]

pigeon%

Guy Decoux

I’m very new to Ruby, I generally work in Python. I am wanting to try
writing a simple little chat server in it. The problem is, I’m trying to
find something in ruby similar to the select() function in C/Python that
returns a list of readable/writeable/error sockets, but I can’t seem to
find one.

Another option might be to use threads.

Bjorn Stahl posted a nice threaded TCPServer example at
http://www.ruby-talk.com/59018

(For a non-threaded example, I posted http://www.ruby-talk.com/59000 -
but please ignore my comments about threading and sockets being
problematic; I was mistaken!)

Regards,

Bill

···

From: “Robert Klemme” bob.news@gmx.net

There’s a simpler example within this page:

(towards the end of section 2) although it doesn’t include the concurrency
limit.

I don’t think you should need to use ‘select’ when writing a threaded
server. If you want a timeout, it’s simpler to do:

require ‘timeout’
status = timeout(5) do
# something which may take time
end

Cheers,

Brian.

···

On Thu, Apr 24, 2003 at 12:09:55AM +0900, Bill Kelly wrote:

Bjorn Stahl posted a nice threaded TCPServer example at
http://www.ruby-talk.com/59018

Thank you all very much.

Kuros

···

----- Original Message -----
From: “Brian Candler” B.Candler@pobox.com
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Wednesday, April 23, 2003 10:20 AM
Subject: Re: Select()

On Thu, Apr 24, 2003 at 12:09:55AM +0900, Bill Kelly wrote:

Bjorn Stahl posted a nice threaded TCPServer example at
http://www.ruby-talk.com/59018

There’s a simpler example within this page:
http://www.rubygarden.org/ruby?SingletonTutorial
(towards the end of section 2) although it doesn’t include the concurrency
limit.

I don’t think you should need to use ‘select’ when writing a threaded
server. If you want a timeout, it’s simpler to do:

require ‘timeout’
status = timeout(5) do
# something which may take time
end

Cheers,

Brian.