From: Vidar Hokstad Date: 2006-11-05T19:10:14+09:00 Subject: Re: Nonblocking IO read 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. > > > > 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.) > > 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? I have a messaging middleware server running right now that is processing millions of messages a day. It's written in Ruby, and does _all_ it's work in a single thread using IO multiplexing with select(). Not only can it be done - it is fairly easy (~700 lines for the entire app, including db persistence support etc.). Doing the same thing with threads and blocking IO, on the other hand, would fall apart horribly due to the way Ruby does threading. Forking also wouldn't be an option as the processes would still need to actually exchange those messages. In fact, internally, Ruby does all it's IO using non-blocking IO exactly because the threading model would cause everything to block otherwise. Incidentally that's also one of the reasons why using threads + blocking IO performs extremely badly in Ruby once the number of threads gets beyond a certain level, because it causes Ruby to call select() far too often. Vidar