Skip to content
This repository


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Sequenced file reader and writer through EventMachine. Solves problem of blocking disk IO while operating with large files.

branch: master

Fetching latest commit…


Cannot retrieve the latest commit at this time

Octocat-spinner-32 lib
Octocat-spinner-32 .document
Octocat-spinner-32 .gitignore
Octocat-spinner-32 Gemfile
Octocat-spinner-32 Gemfile.lock
Octocat-spinner-32 LICENSE.txt
Octocat-spinner-32 Rakefile
Octocat-spinner-32 VERSION
Octocat-spinner-32 em-files.gemspec
Octocat-spinner-32 test.rb

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).

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
            io.close()              # 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.


  1. Fork it.
  2. Create a branch (git checkout -b 20101220-my-change).
  3. Commit your changes (git commit -am "Added something").
  4. Push to the branch (git push origin 20101220-my-change).
  5. Create an Issue with a link to your branch.
  6. Enjoy a refreshing Diet Coke and wait.


Copyright © 2011 Martin Kozák. See LICENSE.txt for further details.

Something went wrong with that request. Please try again.