From: Joshua Haberman Date: 2005-10-03T09:35:51+09:00 Subject: Re: state of blocking/nonblocking I/O --Apple-Mail-5--331309369 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=US-ASCII; delsp=yes; format=flowed Tanaka, Thanks for your helpful answers! On Oct 2, 2005, at 5:11 PM, Tanaka Akira wrote: > In article <17383D97-991D-4947-AB88-85758AB964FB@reverberate.org>, > Joshua Haberman writes: >> - You can try setting O_NONBLOCK on your IO objects with fcntl. That >> will help you in the case where you only have one Ruby thread -- now >> read and write will raise Errno::EAGAIN if the fd isn't ready. >> > > No. > > IO#write doesn't raise Errno::EAGAIN but retry until all data is > written. > > IO#read also retry since Ruby 1.9. > > So IO#write and IO#read may block calling thread. Hrm, so I guess that if I want to do real nonblocking I/O in Ruby, I have to write IO#nonblock_read and IO#nonblock_write, that do not have this retry behavior? >> One other question: are the buffered fread()/fwrite() functions >> guaranteed to work correctly if O_NONBLOCK is set on the underlying >> descriptor? I have not been able to find a good answer to this. >> > > fwrite(3) may lost data. > > So Ruby 1.8 may lost data. > > % ruby-1.8.3 -v > ruby 1.8.3 (2005-09-21) [i686-linux] > % ruby-1.8.3 -rfcntl -e ' > w = STDOUT > w.fcntl(Fcntl::F_SETFL, w.fcntl(Fcntl::F_GETFL) | Fcntl::O_NONBLOCK) > w << "a" * 4096 > w.flush > w << "b" > w.flush > ' | ruby -e 'sleep 1; p STDIN.read.length' > 4096 Ooh, that's bad. What's the explanation for that? >> # this will block our thread, even though the fd is set to >> nonblocking. >> # however, if you eliminate the background thread, this call with >> give you EAGAIN, >> # which is what you want. >> read_pipe.read >> > > If you want to test some data available, use IO.select. Yes, but IO.select can't tell me how *much* data I can read or write. IO#read and IO#write can still block if I try to read or write too much data, which is what I want to avoid. Thanks, Josh --Apple-Mail-5--331309369--