Skip to content

Instantly share code, notes, and snippets.

@curi1119
Created November 11, 2014 01:52
Show Gist options
  • Save curi1119/62ecdf9c2a49ce3156cb to your computer and use it in GitHub Desktop.
Save curi1119/62ecdf9c2a49ce3156cb to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
require 'eventmachine'
EM.run {
puts "Main thread => #{Thread.current}"
EM.add_periodic_timer(5.0){
puts "#{Thread.current} - main thread add_periodic_timer"
}
EM.add_timer(10.0){
puts "#{Thread.current} - add_timer"
puts "#{Thread.current} - I will die."
EM.stop
}
EM.next_tick {
puts "#{Thread.current} - next tick"
}
EM.defer {
puts "#{Thread.current} - defer"
}
EM.defer {
3.times do |i|
puts "#{Thread.current} - defer2 i=#{i}"
sleep 2.0
end
}
EM.defer{
EM.add_periodic_timer(1.0){
puts "#{Thread.current} - add_periodic_timer on defer(should run on main thread)"
}
}
EM.next_tick {
puts "#{Thread.current} - next tick2"
EM.defer {
puts "#{Thread.current} - defer inside next tick"
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment