Last active
December 24, 2019 17:28
-
-
Save havenwood/f7c372fdae5bd53cb7f819bd0c027f45 to your computer and use it in GitHub Desktop.
An example showing time now slipping, as long as job takes less time than interval
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 'async' | |
require 'async/notification' | |
def do_a_thing_lasting(duration) | |
puts 'hi' | |
sleep duration | |
puts 'bye' | |
end | |
def now_and_every(interval) | |
Async do |task| | |
notification = Async::Notification.new | |
task.async do | |
loop do | |
notification.wait | |
yield | |
end | |
end | |
loop do | |
notification.signal | |
task.sleep interval | |
end | |
end | |
end | |
now_and_every(2) { do_a_thing_lasting(1) } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment