Skip to content

Instantly share code, notes, and snippets.

@havenwood
Last active December 24, 2019 17:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save havenwood/f7c372fdae5bd53cb7f819bd0c027f45 to your computer and use it in GitHub Desktop.
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
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