防止从 URL 打开大文件时超时
我正在编写一个 Ruby 1.8.7 脚本,该脚本必须从服务器请求非常大的 XML 文件(1 - 5MB),该文件速度相当慢(1MB 需要 1 分钟 30 秒)。请求的文件被写入磁盘。
我在脚本中将超时设置为一些荒谬的秒数,因为我真的想获取文件,而不是在花费太长时间时继续前进。仍然有大量的秒数,我不断超时。
这有最佳实践吗?
现在,我
open(DIR + "" + number + "" + ".xml", 'wb') do |file|
begin
status = Timeout::timeout(6000000) do
file << open(url).read
end
rescue Timeout::Error => e
Rails.logger.info "Timeout for:" + number.to_s
end
end
现在使用的超时设置以秒为单位,这将使 6000000
远远超过 1 分钟 30 秒,但不知何故,它没有使用我以秒为单位的超时。再次注意,我只能使用 Ruby 1.8.7
I am writing a Ruby 1.8.7 script which has to request really large XML files(1 - 5MB) from server which is quite slow(1min30sec for 1MB). The requested file is written to disk.
I set the timeout in my script to some ridiculous amount of seconds, since I really want to get the file, not just move on if it takes too long. Still with the high amount of seconds I keep getting timeouts.
Is there a best practice for this?
right now I use
open(DIR + "" + number + "" + ".xml", 'wb') do |file|
begin
status = Timeout::timeout(6000000) do
file << open(url).read
end
rescue Timeout::Error => e
Rails.logger.info "Timeout for:" + number.to_s
end
end
now tought timeout was set in seconds which would make 6000000
way more then 1min30sec, but somehow it isn't using my timeout in seconds. Note again that i'm restricted to using Ruby 1.8.7
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不幸的是,这是有问题的。在 Ruby 1.9.x 中,open-uri-extended
open
可以采用read_timeout
参数,并将其传递给 http 库。但在您使用的 Ruby 1.8.x 中,此参数不可用。因此,您需要直接使用 net/http,调用 start/get 并根据您的喜好设置 read_timeout 。如果您只使用 open-uri 包装器,则 read_timeout 仍为 60 秒,这比您想要的要短。
Unfortunately, this is problematic. In Ruby 1.9.x, open-uri-extended
open
can takeread_timeout
parameter, which it passes over to http library. But in Ruby 1.8.x, which you're using, this parameter is not available.So, you need to use net/http directly, call start/get there and set read_timeout to your liking. If you just use the open-uri wrapper, the read_timeout remains 60 seconds, which is shorter than what you want.