I'm trying to watch some (log)files for changes. For this purpose,
non-blocking I/O and the select function look promising, unfortunately,
I don't get them to work.

Here's a snippet of my effort


require "fcntl"

class Logwatch

  def Logwatch.watch()
    testfile = open("/tmp/test", "r")
    testfile.fcntl(Fcntl::F_SETFL, Fcntl::O_NONBLOCK)
    
    while (true)
      readAvail = select([testfile])[0]
      
      readAvail.each() do |f|
        s = f.read();
        if (s != nil)
          puts "-> Reading"
          puts s
        else
          puts "-> Sleeping"
          sleep(5)
        end
      end
    end
  end
end

Logwatch.watch()


In the first iteration through the while-loop, the test file is read up
to EOF; when I then append more lines to it, select wakes up, but
apparently read doesn't read the new lines. What's happening?

Michael

-- 
Michael Schuerig
mailto:schuerig / acm.org
http://www.schuerig.de/michael/