From: Brian Candler Date: 2004-10-05T01:25:13+09:00 Subject: Re: Thinking about Threaded IO On Tue, Oct 05, 2004 at 01:10:48AM +0900, James Edward Gray II wrote: > >'read'? I think that > > > > mybuf.sbuf += s.read(65536) > > > >probably would have worked as you'd expected. > > So with a big read, you can still hang waiting for the bytes? Are you > suggesting read()) would have handled this better than sysread()? > Where does that leave gets()? IO#read and IO#gets work properly; in other words Ruby wraps the calls appropriately to make sure they never block the interpreter engine. If you use sysread then you're telling Ruby to bypass what it knows, and just call the underlying O/S function directly. In that case, you should know what you are doing before you ask for it! Checking with the source, IO#sysread checks the FD is ready (essentially using select()) and then does a single read() operation of the size requested: n = fileno(fptr->f); rb_thread_wait_fd(fileno(fptr->f)); TRAP_BEG; n = read(fileno(fptr->f), RSTRING(str)->ptr, RSTRING(str)->len); TRAP_END; whereas IO#read goes via rb_io_fread, which reads only as much data is available at a time, appending it to a string. IO#gets goes via appendline which also checks how much data is available before reading it. Regards, Brian.