Skip to content

Instantly share code, notes, and snippets.

@evantahler
Last active December 14, 2015 08:49
Show Gist options
  • Save evantahler/5060222 to your computer and use it in GitHub Desktop.
Save evantahler/5060222 to your computer and use it in GitHub Desktop.
Ruby Signal async
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
@evantahler
Copy link
Author

> 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
...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment