Created
April 27, 2012 21:37
-
-
Save jsonperl/2513478 to your computer and use it in GitHub Desktop.
EM defer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
source 'http://rubygems.org' | |
gem 'eventmachine', ">= 1.0.0.beta" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'bundler' | |
Bundler.require | |
class Reactor | |
def self.run | |
@output = [] | |
@count = 0 | |
EM.run do | |
Signal.trap("INT") { EventMachine::stop_event_loop } | |
Signal.trap("TERM") { EventMachine::stop_event_loop } | |
timer = EventMachine::PeriodicTimer.new(0.02) do | |
do_something = Proc.new do | |
count = 0 | |
while count < 100000 | |
count += 1 | |
end | |
end | |
#do_something.call | |
EventMachine.defer do_something | |
@output << "Run time: #{(Time.now - @tick) * 1000}ms" if @tick | |
@tick = Time.now | |
@count += 1 | |
if @count == 50 | |
EventMachine::stop_event_loop | |
puts @output.join("\n") | |
end | |
end | |
end | |
end | |
end | |
Reactor.run |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment