Skip to content

Instantly share code, notes, and snippets.

@pweldon
Forked from ileitch/gist:1459987
Created July 31, 2012 22:38
Show Gist options
  • Save pweldon/3221309 to your computer and use it in GitHub Desktop.
Save pweldon/3221309 to your computer and use it in GitHub Desktop.
Interruptible sleep in Ruby
module InterruptibleSleep
def interruptible_sleep(seconds)
@_sleep_check, @_sleep_interrupt = IO.pipe
IO.select([@_sleep_check], nil, nil, seconds)
end
def interrupt_sleep
@_sleep_interrupt.close if @_sleep_interrupt
end
end
class Test
include InterruptibleSleep
def start
puts "start"
interruptible_sleep 10
puts "sleep interrupted!"
end
def stop
puts "stop"
interrupt_sleep
end
end
test = Test.new
Signal.trap('SIGINT') { test.stop }
test.start
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment