Created
August 8, 2008 12:06
-
-
Save tmm1/4563 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 '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