Last active
December 14, 2015 08:49
-
-
Save evantahler/5060222 to your computer and use it in GitHub Desktop.
Ruby Signal async
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
def sleeper | |
i = 0 | |
while i < 10 | |
puts "sleeping... #{i}" | |
blocking_sleep 1 | |
i = i + 1 | |
end | |
return i | |
end | |
def blocking_sleep(seconds) | |
start = Time.new.to_i | |
while Time.new.to_i - seconds <= start | |
#loop | |
end | |
end | |
def main | |
deferred_action_call | |
sleep_count = sleeper | |
puts "total sleep seconds: #{sleep_count}" | |
end | |
######## | |
def deferred_action_call | |
Process.kill :USR2, Process.pid | |
end | |
def deferred_action_catch | |
puts "got callback" | |
i = 0 | |
while i < 5 | |
puts "callback #{i}" | |
sleep 1 | |
i = i + 1 | |
end | |
end | |
######## | |
Signal.trap("USR2") do | |
deferred_action_catch | |
end | |
main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
> ruby delay.rb sleeping... 0 got callback callback 0 callback 1 callback 2 callback 3 callback 4 sleeping... 1 sleeping... 2 sleeping... 3 sleeping... 4 sleeping... 5 sleeping... 6 sleeping... 7 sleeping... 8 sleeping... 9 total sleep seconds: 10 ...