Hi,
In message "Re: Unable to do non-blocking read on socket"
on 03/03/03, Seth Kurtzberg <seth / cql.com> writes:
|> (a) IO#read (without size) returns all available data on the IO.
|> It doesn't block if O_NONBLOCK is set. I was wrong in
|> [ruby-talk:66149].
|
|Not what I would like to see but not what I was trying to use.
Could you describe "what you'd like to see"? I'm having (sort of)
hard time to understand you.
By the way, it somewhat different from the documentation:
---------------------------------------------------------------- IO#read
ios.read( [anInteger] ) -> aString or nil
------------------------------------------------------------------------
Reads at most anInteger bytes from the I/O stream, or to the end of
file if anInteger is omitted. Returns nil if called at end of file.
f = File.new("testfile")
f.read(16) #=> "This is line one"
what do you guys think about this behavior. Should it be fixed as
the document says, or should we update the document?
|> (b) IO#read (with maximum size) raise an EWOULDBLOCK exception if the
|> buffer is empty. It returns the buffer contents if the buffer is
|> partially filled before EWOULDBLOCK.
|
|It never returns EWOULDBLOCK. Unless the buffer is empty at the time of the
|call, it blocks until the maximum number of bytes (as specified by the
|parameter) is received.
|
|So, both variations block, and NONBLOCK behavior is not available with either.
?
On my machine, ruby 1.8.0 (2003-02-28) [i686-linux], (a) doesn't
block, since it returns as much as data available without blocking if
O_NONBLOCK is set. And (b) doesn't block neither, since it either
raises the exception, or returns data available on the buffer.
(a) is proved by Nobu's example [ruby-talk:66126].
(b) can be proved by replacing the line in the example
p s.nonblock {sleep WAIT; s.read}
by
s.nonblock {sleep WAIT; p s.read(40); p s.read(40)}
and try
% ruby client.rb foo 2
you'll get
"hello,"
/tmp/client.rb:8:in `read': Resource temporarily unavailable - "/tmp/foo" (Errno::EWOULDBLOCK)
from /tmp/client.rb:8
from /tmp/client.rb:8:in `nonblock'
from /tmp/client.rb:8
matz.