views:

227

answers:

2

I have two scripts which using mechanize to fetch google index page. I assuming Eventmachine will faster than ruby thread, but not.

Eventmachine code cost "0.24s user 0.08s system 2% cpu 12.682 total"

Ruby Thread code cost "0.22s user 0.08s system 5% cpu 5.167 total "

Am I use eventmachine in wrong way?

Who can explain it to me, thanks!

1 Eventmachine

require 'rubygems'
require 'mechanize'
require 'eventmachine'

trap("INT") {EM.stop}

EM.run do 
  num = 0
  operation = proc {
    agent = Mechanize.new
    sleep 1
    agent.get("http://google.com").body.to_s.size
  }
  callback = proc { |result|
    sleep 1
    puts result
    num+=1
    EM.stop if num == 9
  }

  10.times do 
    EventMachine.defer operation, callback
  end
end

2 Ruby Thread

require 'rubygems'
require 'mechanize'


threads = []
10.times do 
  threads << Thread.new do 
    agent = Mechanize.new
    sleep 1
    puts agent.get("http://google.com").body.to_s.size
    sleep 1
  end
end


threads.each do |aThread| 
  aThread.join
end
+4  A: 

Yep, you're using it wrong. EventMachine works by making asynchronous IO calls that return immediately and notify the "reactor" (the event loop started by EM.run) when they are completed. You have two blocking calls that defeat the purpose of the system, sleep and Mechanize.get. You have to use special asynchronous/non-blocking libraries to derive any value from EventMachine.

Ben Hughes
+1  A: 

You should use something like em-http-request http://github.com/igrigorik/em-http-request

Edgar