2010-03-12 00:37:16 +03:00
|
|
|
require 'timeout'
|
|
|
|
|
|
|
|
def progress(n = 5)
|
2018-03-22 16:01:17 +03:00
|
|
|
n.times {|i| print i; STDOUT.flush; sleep 1}
|
2010-03-12 00:37:16 +03:00
|
|
|
puts "never reach"
|
|
|
|
end
|
|
|
|
|
2018-03-16 17:09:40 +03:00
|
|
|
p Timeout.timeout(5) {
|
2010-03-12 00:37:16 +03:00
|
|
|
45
|
|
|
|
}
|
2018-03-16 17:09:40 +03:00
|
|
|
p Timeout.timeout(5, Timeout::Error) {
|
2010-03-12 00:37:16 +03:00
|
|
|
45
|
|
|
|
}
|
2018-03-16 17:09:40 +03:00
|
|
|
p Timeout.timeout(nil) {
|
2010-03-12 00:37:16 +03:00
|
|
|
54
|
|
|
|
}
|
2018-03-16 17:09:40 +03:00
|
|
|
p Timeout.timeout(0) {
|
2010-03-12 00:37:16 +03:00
|
|
|
54
|
|
|
|
}
|
|
|
|
begin
|
2018-03-16 17:09:40 +03:00
|
|
|
Timeout.timeout(5) {progress}
|
2010-03-12 00:37:16 +03:00
|
|
|
rescue => e
|
|
|
|
puts e.message
|
|
|
|
end
|
|
|
|
begin
|
2018-03-16 17:09:40 +03:00
|
|
|
Timeout.timeout(3) {
|
2010-03-12 00:37:16 +03:00
|
|
|
begin
|
2018-03-16 17:09:40 +03:00
|
|
|
Timeout.timeout(5) {progress}
|
2010-03-12 00:37:16 +03:00
|
|
|
rescue => e
|
|
|
|
puts "never reach"
|
|
|
|
end
|
|
|
|
}
|
|
|
|
rescue => e
|
|
|
|
puts e.message
|
|
|
|
end
|
|
|
|
class MyTimeout < StandardError
|
|
|
|
end
|
|
|
|
begin
|
2018-03-16 17:09:40 +03:00
|
|
|
Timeout.timeout(2, MyTimeout) {progress}
|
2010-03-12 00:37:16 +03:00
|
|
|
rescue MyTimeout => e
|
|
|
|
puts e.message
|
|
|
|
end
|