How does IO buffering work in Ruby? How often is data flushed to the underlying stream when using the IO and File classes? How does this compare to OS buffering? What needs to be done to guarantee that given data has been written to disk, before confidently reading it back for processing?
Understanding Ruby and OS I/O buffering
13.3k Views Asked by jrdioko At
2
There are 2 best solutions below
0
Björn Nilsson
On
Ruby does its internal buffering on top of the OS. When you do file.flush Ruby flushes its internal buffer. To ensure the file is written to disk you need to do file.fsync. But in the end you can not be certain the file is written to disk anyway, it depends on the OS, the hdd controller and the hdd.
Related Questions in RUBY
- Best way to make an HABTM association via console
- undefined method `namespace' for main:Object (NoMethodError) - active record / rakefile
- Ruby destroy is not working? Or objects still present?
- Trying to set the value of an input with mechanize
- How to split the logic in a ruby game
- How can I monitor an endpoint's status with Ruby?
- Why can a private class method be explicitly invoked in Ruby?
- Rails - Ajax do not work properly on production server
- syntax error, unexpected kEND
- Carrierwave file upload with different file types
- b.javascript_dialog().exists? is not working for me in WATIR 4.0.2
- Combine two arrays of hashes
- Building a simple calculator form in Rails 4
- How do I update create route from rails 3 to 4
- Comparison of Fixnum with nil failed - palindrome program Ruby
Related Questions in OPERATING-SYSTEM
- Why two threads accessing one resource crashes one thread?
- How to tell the difference between linux and mac
- Can a single thread be shared among multiple processes ? If yes how?
- /usr/lib/* files had been deleted, how to restore these files
- What does a POSIX interface refer to in terms of microkernels?
- Is zero copy principle supported in Mac
- Why segment files into chunks for HTTP streaming?
- Add/remove process from kernel runqueue
- How does my computer know to which character a char corresponds?
- Who starts the OS process scheduler?
- ^M behind operating system version?
- How to make a scanf() type function in a 32bit os in c?
- How is `dup2` actually working?
- Logged in hostname/IP in linux command history
- Had 16-bit DOS a memory access limitation of 1 MB? If yes, how?
Related Questions in IO
- Java listFiles in directory in jar
- C++ cin can't read in integers with 0 in them
- C++ reading a file into a struct
- What is meant by Streams w.r.t Java IO
- Blender Python Script Deleting Meshes
- C++ not reading anything from files
- Output EOF using %f
- how to write the output of iostream to buffer, python3
- Direct chart plotting Pandas DataFrame columns to Xlsxwriter in a loop
- Why is it slower to print directly to console/terminal than redirecting?
- withDefaultPrettyPrinter() doesn't make the output be formatted
- How fast can we make a specific tr?
- How to grep a string in a program?
- Why does grep give "Binary file (standard input) matches"?
- Trying to use output of one function to influence the next function to count words in text file
Related Questions in BUFFERING
- Getting output from `exec.Cmd` in "real-time"
- Can you upload to S3 using a stream rather than a local file?
- What is the potential benefit of allowing C++ and C streams to buffer independently?
- Python: why is this print automatically flushing to screen without flush()?
- why is rendering turning form black
- Python: contextlib.redirect_stdout sometimes doesn't work in real time
- Android API 23 vs Android API 25 in MediaPlayer buffering speed
- Is there a way to output View without buffering in Kohana?
- Perfect V-sync implementation for a lightweight OpenGL game: need one tidbit of information
- Response Buffer Limit Exceeded
- Is there any standard solution for buffering output operations in C++?
- buffer confusion
- Does recv remove packets from pcaps buffer?
- Strange IOException when buffering inputStream in Java
- Reading two Characters in C#
Related Questions in IO-BUFFERING
- Using user-buffered I/O for File operations
- Is data from udp socket buffered until it is read for processing
- SQL server - high buffer IO and network IO
- My wireless interface seems to buffer incoming messages and receive them periodically
- Real time read from subprocess.stdout on Windows
- Can I stop std::cout flushing on "\n"?
- How do I flush a file in Perl?
- Streaming web uploads to socket with Rack
- Cygwin terminal buffers STDOUT
- Is file buffering by OS harmful?
- Does calling CloseHandle on a file handle open for writing imply FlushFileBuffers too?
- Understanding Ruby and OS I/O buffering
- Is there any way to find the buffer size of a file object
- What is the difference between the buffering argument to open() and the hardcoded readahead buffer size used when iterating through a file?
- reasons why pipe buffering not working correctly
Trending Questions
- UIImageView Frame Doesn't Reflect Constraints
- Is it possible to use adb commands to click on a view by finding its ID?
- How to create a new web character symbol recognizable by html/javascript?
- Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
- Heap Gives Page Fault
- Connect ffmpeg to Visual Studio 2008
- Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
- How to avoid default initialization of objects in std::vector?
- second argument of the command line arguments in a format other than char** argv or char* argv[]
- How to improve efficiency of algorithm which generates next lexicographic permutation?
- Navigating to the another actvity app getting crash in android
- How to read the particular message format in android and store in sqlite database?
- Resetting inventory status after order is cancelled
- Efficiently compute powers of X in SSE/AVX
- Insert into an external database using ajax and php : POST 500 (Internal Server Error)
Popular Questions
- How do I undo the most recent local commits in Git?
- How can I remove a specific item from an array in JavaScript?
- How do I delete a Git branch locally and remotely?
- Find all files containing a specific text (string) on Linux?
- How do I revert a Git repository to a previous commit?
- How do I create an HTML button that acts like a link?
- How do I check out a remote Git branch?
- How do I force "git pull" to overwrite local files?
- How do I list all files of a directory?
- How to check whether a string contains a substring in JavaScript?
- How do I redirect to another webpage?
- How can I iterate over rows in a Pandas DataFrame?
- How do I convert a String to an int in Java?
- Does Python have a string 'contains' substring method?
- How do I check if a string contains a specific word?
The Ruby IO documentation is not 100% clear on how this buffering works, but this is what you can extract from the documentation:
The relevant methods to look at:
IO.flush: FlushesIO. I also looked at the Ruby source and a call toIO.flushalso calls the underlying OSfflush(). This should be enough to get the file cached, but does not guarantee physical data to disk.IO.sync=: If set totrue, no Ruby internal buffering is done. Everything is immidiately sent to the OS, andfflush()is called for each write.IO.sync: Returns the current sync setting (trueorfalse).IO.fsync: Flushes both the Ruby buffers + callsfsync()on the OS (if it supports it). This will guarantee a full flush all the way to the physical disk file.IO.close: Closes the RubyIOand writes pending data to the OS. Note that this does not implyfsync(). The POSIX documentation onclose()says that it does NOT guarantee data is physically written to the file. So you need to use an explicitfsync()call for that.Conclusion:
flushand/orcloseshould be enough to get the file cached so that it can be read fully by another process or operation. To get the file all the way to the physical media with certainty, you need to callIO.fsync.Other related methods:
IO.syswrite: Bypass Ruby internal buffers and do a straight OSwrite. If you use this then do not mix it withIO.read/write.IO.sysread: Same as above, but for reading.