1999-08-13 09:45:20 +04:00
|
|
|
#
|
|
|
|
# timeout.rb -- execution timeout
|
|
|
|
#
|
2000-05-01 13:42:38 +04:00
|
|
|
# Copyright (C) 2000 Network Applied Communication Laboratory, Inc.
|
2000-05-09 08:53:16 +04:00
|
|
|
# Copyright (C) 2000 Information-technology Promotion Agency, Japan
|
2000-05-01 13:42:38 +04:00
|
|
|
#
|
1999-08-13 09:45:20 +04:00
|
|
|
#= SYNOPSIS
|
|
|
|
#
|
|
|
|
# require 'timeout'
|
|
|
|
# status = timeout(5) {
|
|
|
|
# # something may take time
|
|
|
|
# }
|
|
|
|
#
|
|
|
|
#= DESCRIPTION
|
|
|
|
#
|
|
|
|
# timeout executes the block. If the block execution terminates successfully
|
|
|
|
# before timeout, it returns true. If not, it terminates the execution and
|
|
|
|
# raise TimeoutError exception.
|
|
|
|
#
|
|
|
|
#== Parameters
|
|
|
|
#
|
|
|
|
# : timout
|
|
|
|
#
|
* dln.c, io.c, pack.c, lib/benchmark.rb, lib/cgi.rb, lib/csv.rb,
lib/date.rb, lib/ftools.rb, lib/getoptlong.rb, lib/logger.rb,
lib/matrix.rb, lib/monitor.rb, lib/set.rb, lib/thwait.rb,
lib/timeout.rb, lib/yaml.rb, lib/drb/drb.rb, lib/irb/workspace.rb,
lib/net/ftp.rb, lib/net/http.rb, lib/net/imap.rb, lib/net/pop.rb,
lib/net/telnet.rb, lib/racc/parser.rb, lib/rinda/rinda.rb,
lib/rinda/tuplespace.rb, lib/shell/command-processor.rb,
lib/soap/rpc/soaplet.rb, lib/test/unit/testcase.rb,
lib/test/unit/testsuite.rb: typo fix.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6178 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-04-19 03:19:47 +04:00
|
|
|
# The time in seconds to wait for block termination.
|
1999-08-13 09:45:20 +04:00
|
|
|
#
|
2003-07-03 15:02:53 +04:00
|
|
|
# : [exception]
|
|
|
|
#
|
* dln.c, io.c, pack.c, lib/benchmark.rb, lib/cgi.rb, lib/csv.rb,
lib/date.rb, lib/ftools.rb, lib/getoptlong.rb, lib/logger.rb,
lib/matrix.rb, lib/monitor.rb, lib/set.rb, lib/thwait.rb,
lib/timeout.rb, lib/yaml.rb, lib/drb/drb.rb, lib/irb/workspace.rb,
lib/net/ftp.rb, lib/net/http.rb, lib/net/imap.rb, lib/net/pop.rb,
lib/net/telnet.rb, lib/racc/parser.rb, lib/rinda/rinda.rb,
lib/rinda/tuplespace.rb, lib/shell/command-processor.rb,
lib/soap/rpc/soaplet.rb, lib/test/unit/testcase.rb,
lib/test/unit/testsuite.rb: typo fix.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6178 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-04-19 03:19:47 +04:00
|
|
|
# The exception class to be raised on timeout.
|
2003-07-03 15:02:53 +04:00
|
|
|
#
|
1999-08-13 09:45:20 +04:00
|
|
|
#=end
|
|
|
|
|
2003-07-03 15:02:53 +04:00
|
|
|
module Timeout
|
|
|
|
class Error<Interrupt
|
|
|
|
end
|
1999-08-13 09:45:20 +04:00
|
|
|
|
2003-07-03 15:02:53 +04:00
|
|
|
def timeout(sec, exception=Error)
|
2003-07-29 22:26:55 +04:00
|
|
|
return yield if sec == nil or sec.zero?
|
2003-07-03 15:02:53 +04:00
|
|
|
begin
|
|
|
|
x = Thread.current
|
|
|
|
y = Thread.start {
|
|
|
|
sleep sec
|
|
|
|
x.raise exception, "execution expired" if x.alive?
|
|
|
|
}
|
|
|
|
yield sec
|
|
|
|
# return true
|
|
|
|
ensure
|
|
|
|
y.kill if y and y.alive?
|
|
|
|
end
|
1999-08-13 09:45:20 +04:00
|
|
|
end
|
2003-07-03 15:02:53 +04:00
|
|
|
module_function :timeout
|
|
|
|
end
|
|
|
|
|
|
|
|
# compatible
|
2003-07-03 18:44:46 +04:00
|
|
|
def timeout(n, e=Timeout::Error, &block)
|
|
|
|
Timeout::timeout(n, e, &block)
|
1999-08-13 09:45:20 +04:00
|
|
|
end
|
2003-07-03 15:02:53 +04:00
|
|
|
TimeoutError = Timeout::Error
|
2000-10-14 18:44:58 +04:00
|
|
|
|
|
|
|
if __FILE__ == $0
|
2001-08-20 08:29:58 +04:00
|
|
|
p timeout(5) {
|
|
|
|
45
|
|
|
|
}
|
2003-07-03 18:44:46 +04:00
|
|
|
p timeout(5, TimeoutError) {
|
|
|
|
45
|
|
|
|
}
|
2003-07-29 22:26:55 +04:00
|
|
|
p timeout(nil) {
|
|
|
|
54
|
|
|
|
}
|
|
|
|
p timeout(0) {
|
|
|
|
54
|
|
|
|
}
|
2001-08-20 08:29:58 +04:00
|
|
|
p timeout(5) {
|
|
|
|
loop {
|
|
|
|
p 10
|
|
|
|
sleep 1
|
|
|
|
}
|
2000-10-14 18:44:58 +04:00
|
|
|
}
|
|
|
|
end
|