eventmachine snippets

How to install thin, rack and eventmachine on Windows/Cygwin

Tagged rack, eventmachine, windows, cygwin, thin  Languages bash

There's no official Windows binary release of eventmachine for the latest version of thin (1.0.0). Thin needs at least version (0.12.2) of eventmachine, so you'll have to use the latest GitHub code (currently 0.12.3) to install thin on Windblows and CygWin:

git clone git://github.com/eventmachine/eventmachine.git
cd eventmachine
rake gem
gem install pkg/eventmachine-0.12.3.gem
gem install thin rack

How to fetch URLs in parallell with EventMachine and Ruby

Tagged eventmachine, ruby, asynchronous, job  Languages ruby

Save time by doing things in parallell:

require 'rubygems'
require 'eventmachine'
require 'open-uri'
require 'pp'
require 'thread'
require 'benchmark'

class Worker
  include EM::Deferrable

  def run
    get_google
    set_deferred_status :succeeded
  end
end

def get_google
  # sorry for spamming you
  open('http://www.google.com/') do |f|
    #pp f.meta
  end
end

def asynchronous(i)
  worker = Worker.new
  # on success
  worker.callback do
    p "#{Thread.current} done #{i}!"
  end 
  worker.errback do 
    p "Unexpected error"    
    EM.stop  
  end
  #
  Thread.new do
    worker.run
    EM.stop
  end 
  #puts "scheduling done!"
end

def synchronous(i)
  get_google
end

# on error
EM.error_handler do |e|  
  p "Unexpected error: #{e}" 
end

EM.run do
  seconds = Benchmark.realtime do
    50.times do |i|
      asynchronous i
    end
  end
  p "With EventMachine: #{seconds} elapsed..."

  seconds = Benchmark.realtime do
    50.times do |i|
      synchronous i
    end
  end
  p "Without EventMachine: #{seconds} elapsed..."
end

Output:

With EventMachine: 9.05974316596985 elapsed...
Without EventMachine: 19.1381118297577 elapsed...

Conclusion

* Speeds up blocking operations. * EventMachine is currently limited to one CPU core (native thread) per process.

References * http://ujihisa.blogspot.com/2009/08/try-eventmachine.html * http://www.scribd.com/doc/21017871/Event-Machine-Presentation

* http://www.scribd.com/doc/25939580/Event-Machine

Eventmachine Echo Server Example

Tagged eventmachine, echo, server, tcp  Languages ruby
require 'rubygems'
require 'eventmachine'
require 'logger'

#
# Each connection creates a new EchoServer.
#
module EchoServer
  LOG = Logger.new('echo.log')

  #
  # Called by the event loop immediately after the network connection has been
  # established, and before resumption of the network loop.
  #
  def post_init
    LOG.info "-- Connection established #{remote_ip} --"
  end

  #
  # Called by the event loop whenever data has been received by the network
  # connection. It is never called by user code.
  #
  def receive_data data
    message = "#{remote_ip}: #{data}"
    LOG.info message
    send_data message
    close_connection if data =~ /quit/i
  end

  #
  # Called by the framework whenever a connection (either a server or client
  # connection) is closed.
  #
  def unbind
    LOG.info "-- Connection closed #{remote_ip} --"
  end

  #
  # Return the IP and port of the remote client.
  #
  def remote_ip
    @remote_ip ||= begin
                     port, ip = Socket.unpack_sockaddr_in(get_peername)
                     "#{ip}:#{port}"
                   end
  end
end

EventMachine::run do
  Signal.trap("INT")  { EventMachine.stop }
  Signal.trap("TERM") { EventMachine.stop }
  EventMachine::start_server "0.0.0.0", 8080, EchoServer
end

Use telnet to test it:

$ telnet localhost 8080