Skip to content

Instantly share code, notes, and snippets.

@tmm1
Created August 8, 2008 12:06
Show Gist options
  • Save tmm1/4563 to your computer and use it in GitHub Desktop.
Save tmm1/4563 to your computer and use it in GitHub Desktop.
require 'thread'
Thread.abort_on_exception = true
require 'rubygems'
require 'eventmachine'
class AsyncTime
def self.now
EM.add_timer(2){ yield(Time.now) }
end
end
class SyncTime
def self.now
raise 'must run in separate thread' if Thread.current == Thread.main
q = Queue.new
AsyncTime.now{|t| q.push(t) }
q.pop
end
end
EM.run{
EM.add_timer(1){ p [:timer, Time.now] }
Thread.new{ p [:sync, SyncTime.now] }
AsyncTime.now{ |t| p [:async, t] }
EM.add_timer(2){ EM.stop }
}
__END__
[:timer, Thu Aug 07 23:09:37 -0700 2008]
[:sync, Thu Aug 07 23:09:38 -0700 2008][:async, Thu Aug 07 23:09:38 -0700 2008]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment