IO::Reactor

IO::Reactor is a pure-Ruby implementation of an asynchronous multiplexed IO Reactor which is based on the Reactor design pattern found in Pattern-Oriented Software Architecture, Volume 2: Patterns for Concurrent and Networked Objects. It allows a single thread to demultiplex and dispatch events from one or more IO objects to the appropriate handler.

I would greatly appreciate feedback on any aspect of this software. Suggestions, feature requests, questions, design critiques, and bug reports are most welcome. Relevant patches and minimal test cases are particularly helpful. I may be reached by email at ged@…, or you can file a ticket.

Trivial Example

This is a very trivial example -- in most circumstances you'd only use a Reactor when you're trying to manage reading and writing on more than a single IO object.

require 'io/reactor'

reactor = IO::Reactor.new
data_to_send = "some stuff to send"

reader, writer = IO.pipe

# Read from the reader end of the pipe until the writer finishes
reactor.register( reader, :read ) do |io,event|
    if io.eof?
        reactor.unregister( io )
        io.close
    else
        puts io.read( 256 )
    end
end

# Write to the writer end of the pipe until there's no data left
reactor.register( writer, :write ) do |io,event|
    bytes = io.write( data_to_send )
    data_to_send.slice!( 0, bytes )

    if data_to_send.empty?
        reactor.unregister( io )
        io.close
    end
end

# Now pump the reactor until both sides are done
puts "Starting IO"
reactor.poll until reactor.empty?
puts "done, exiting."

See the examples directory for some working, more full-featured examples.

Requirements

  • Ruby >= 1.8.6

Optional:

  • RSpec, if you want to run the tests.

Installation

If you use RubyGems?, you can install via:

$ sudo gem install io-reactor

You can also install it as a site library via the Rakefile:

$ wget http://deveiate.org/code/io-reactor-x.y.z.tar.gz
$ tar xzvf io-reactor-x.y.z.tar.gz
$ cd io-reactor-x.y.z
$ sudo rake install

Further Resources

For an index of local wiki pages, see the TitleIndex.