Ruby open-uri RuntimeError - 禁止重定向

发布于 2024-12-09 00:40:11 字数 592 浏览 5 评论 0原文

关于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

塔塔猫 2024-12-16 00:40:11

没关系,愚蠢的错误。我将 begin..rescue 块放入 Thread.do 块中,它起作用了。

Never mind, silly mistake. I put the begin..rescue block inside the Thread.do block and it worked.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文