Ruby open-uri RuntimeError - 禁止重定向
关于 Ruby 错误处理的小问题。我有一些代码大致类似于以下内容:
urls.each do |url|
begin
threads << Thread.new(url) do |url|
page = open(url)
# some further processing of page
end
rescue
puts "Could not retrieve url"
end
threads.each { |thread| thread.join }
但是,当我运行此代码时,我偶尔会遇到重定向的 URL,从而产生以下错误:
/usr/lib/ruby/1.8/open-uri.rb:174:in `open_loop': redirection forbidden: // url goes here
from my_file.rb in 'join'
from my_file.rb in 'each'
最后两行引用包含threads.each 块的代码行。
我只是想知道为什么考虑到我有一个开始救援块,我会收到此错误?我在这里缺少一些微妙的东西,也许与多线程有关?
Little question about Ruby error handling. I have some code that roughly resembles the following:
urls.each do |url|
begin
threads << Thread.new(url) do |url|
page = open(url)
# some further processing of page
end
rescue
puts "Could not retrieve url"
end
threads.each { |thread| thread.join }
However, when I run this I occasionally come across URLs that redirect, producing the following error:
/usr/lib/ruby/1.8/open-uri.rb:174:in `open_loop': redirection forbidden: // url goes here
from my_file.rb in 'join'
from my_file.rb in 'each'
with the two last lines referring to the line of code containing the threads.each block.
I was just wondering why I am getting this error considering I have a begin-rescue block in place? Is there something subtle I'm missing here, perhaps to do with the multithreading?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没关系,愚蠢的错误。我将 begin..rescue 块放入 Thread.do 块中,它起作用了。
Never mind, silly mistake. I put the begin..rescue block inside the Thread.do block and it worked.