Skip to content

Instantly share code, notes, and snippets.

@jsonperl
Created April 27, 2012 21:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jsonperl/2513478 to your computer and use it in GitHub Desktop.
Save jsonperl/2513478 to your computer and use it in GitHub Desktop.
EM defer
source 'http://rubygems.org'
gem 'eventmachine', ">= 1.0.0.beta"
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