Tom Pollard
11/1/2006 12:08:00 AM
On Oct 31, 2006, at 6:23 PM, ara.t.howard@noaa.gov wrote:
> On Wed, 1 Nov 2006 srobertjames@gmail.com wrote:
>> How can I perform a nonblocking IO read? That is, read whatever
>> is there to read and return. If there's nothing yet, just return.
That's what the Unix read() function does ("man 2 read"), available
in Ruby as IO#sysread(). The ri documentation doesn't explain much
about how to use Ruby's sysread, unfortunately.
>> Failing that, is there a way to manually check if there's anything
>> to read or the read will block?
>>
>> (Note that I'm developing for both 'nix and Windows.)
The select() function (Kernel#select, in Ruby) is the standard way to
do IO multiplexing under Unix. You give it a list of file handles on
which you're waiting to be able to read or write, and it returns when
one or more of them is ready. Windows provides a select() function,
too, but it only supports sockets (not pipes or ordinary file IO.)
The Win32 API generally doesn't provide non-blocking IO methods,
because it's assumed you'll use threads if you want to do something
else while waiting for IO.
> it can't be done. search the archives, this sort of thing almost
> always
> indicates a design flaw. for instance - what will your program do
> if there is
> no input?
Check other IO channels, wait for other sorts of events, do work -
whatever. Where do come from asserting that the use of non-blocking
IO represents a "design flaw"?
Tom