Sequenced file reader and writer through EventMachine. Solves problem of blocking disk IO while operating with large files.
Latest commit 177666c Jul 19, 2015 Martin Poljak typo
Failed to load latest commit information.
lib 'hash-utils' dependency removed Jul 18, 2015
.document jeweler files, readme Feb 27, 2011
Gemfile 'hash-utils' dependency removed Jul 18, 2015
LICENSE.txt typo Jul 19, 2015
em-files.gemspec bump to 0.2.4 Jul 18, 2015

EventMachine Files

em-files solve problem of blocking disk IO when operating with large files. Use EventMachine for multiplexing reads and writes to small blocks performed in standalone EM ticks. They speed down the file IO operations of sure, but allow running other tasks with them simultaneously (from EM point of view).

There is, of sure, question whether this all has sense as EM::defer is available for handling these blocking tasks. But sometimes are situations, in which it's undesirable to execute them in separate thread.

API is similar to classic Ruby file IO represented by File class. See an example:

require "em-files"
EM::run do
    EM::File::open("some_file.txt", "r") do |io| do |data|     # writing works by very similar
                                    # way, of sure
            puts data
            # it's necessary to do it in block too, because reading
            # is evented

Support of Ruby API is limited to #open, #close, #read and #write methods only, so for special operations use simply:

EM::File::open("some_file.txt", "r") do |io|
    io.native   # returns native Ruby File class object

Special Uses

It's possible to use also another IO objects than File object by giving appropriate IO instance instead of filename to methods:

require "em-files"
require "stringio"

io = StringIO::new

EM::run do
    EM::File::open(io) do |io|
        # some multiplexed operations

By this way you can also perform for example more time consuming operations by simple way (if they can be processed in block manner) using filters:

require "em-files"
require "zlib"

zip = Zlib::Deflate::new
filter = Proc::new { |chunk| zip.deflate(chunk, Zlib::SYNC_FLUSH) }
data = "..."    # some data bigger than big

EM::run do
    EM::File::write(data, filter)   # done in several ticks

#write supports also copying data from another IO stream because it uses StringIO internally. Simply give it IO object instead of String. It will read it until EOF will occur.


Copyright © 2011 – 2015 Martin Poljak. See LICENSE.txt for further details.