在 Windows XP 上使用 Ruby 进行系统调用时出现超时问题
下面的代码
require 'timeout'
begin
timeout(20) do # Line 4
result = `hostname`
end # Line 6
rescue Timeout::Error
puts "Timeout"
exit
end
puts "Result:" + result # Line 12
会抛出错误
issue.rb:12:in
': 未定义的局部变量或方法
结果' main:Object (NameError)
但如果我注释掉超时元素(第 4 行和第 6 行),它就可以正常工作。我尝试过使用 IO.popen、IO.select 等,但这些都没有帮助。我在许多其他领域使用过这种超时技术,并且效果很好。
它似乎与超时值无关,因为我已经尝试过更大和更小的值。
我在 Windows XP 上使用 Ruby 1.92。非常感谢任何帮助。
ps 我最初的问题不是运行“主机名”,而是运行更复杂的 SQL Server 批处理作业。作为奖励点,长时间运行的系统任务超过超时时间会被自动终止吗?我读过很多关于超时库在忙于运行系统任务时不遵守超时的文章?
The following code
require 'timeout'
begin
timeout(20) do # Line 4
result = `hostname`
end # Line 6
rescue Timeout::Error
puts "Timeout"
exit
end
puts "Result:" + result # Line 12
throws the error
issue.rb:12:in
<main>': undefined local variable or method
result' for
main:Object (NameError)
but if I comment out the timeout element (lines 4 and 6), it works fine. I have tried using IO.popen, IO.select etc but none of this helps. I've used this timeout technique in many other areas and it worked fine.
It doesn't appear to be related to the timeout value as I have experimented with much larger and smaller values.
Am using Ruby 1.92 on Windows XP. Any help much appreciated.
p.s. My original problem was not running "hostname" but a more complex SQL Server batch job. As a bonus point, would a long running system task that exceeded the timeout be automatically killed? I have read lots of posts about the timeout library not honouring timeouts when busy running system tasks?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
result
变量是在超时块内定义的,因此它在外部作用域中不可见。您应该先初始化它:The
result
variable is being defined inside the timeout block, so it's not visible in the outer scope. You should initialize it before: