From: Joel VanderWerf Date: 2010-07-22T09:32:56+09:00 Subject: Re: Writing to file delay Eric Wong wrote: > Matt Triviski wrote: >> Hello All, >> >> I am having a very annoying issue. This is the situation: >> >> I am reading a file on one machine that is continuosly being written to >> and writing out to a socket. On another machine I am reading from the >> socket and writing it to a file. The problem is that when I tail the >> original file and the file I am creating, the new file has a delay of >> around 1 minute until I see the new data. It seems that the OS (Linux >> Redhat 5) is waiting until it gets a block of data before it actually >> writes to the file. I want it to write immediately. >> >> Here is a code snippet of how I am reading from the socket and writing >> to the file. >> >> file = File.new(fname, "w") >> file.sync = true > > Hi Matt, > > "file.sync = true" looks good already. You could try calling fsync as well (it's not a writer, just call "file.fsync"): Implementation from IO ------------------------------------------------------------------------------ ios.fsync -> 0 or nil ------------------------------------------------------------------------------ Immediately writes all buffered data in ios to disk. Note that fsync differs from using IO#sync=. The latter ensures that data is flushed from Ruby's buffers, but doesn't not guarantee that the underlying operating system actually writes it to disk. NotImplementedError is raised if the underlying operating system does not support fsync(2).