我制作了一个程序,可以并行解析文本文件和下载数据。在 9 个或更少线程中运行下载方法时,程序没有错误。但是当在 10 个或更多线程中运行该方法时,程序会抛出“`initialize': getaddrinfo: Name or service not known (SocketError)”错误。我尝试了一些算法并行运行,但出现了同样的问题。我将在发生“名称或服务未知”错误时传递给“打开”方法(open-uri)的 url 放入浏览器并确认该 url 有效并接收到正确的数据。这是部分代码。
jobs = []
aps = []
....
#jobs are pushed into jobs[]
....
max_thread = 15
loop do
ary_threads = []
max_thread.times do |i|
break if jobs.size == 0
job = jobs.pop
ary_threads << Thread.start {
begin
request(job[0],job[1]).each do |ap| #in "request" method, open(url)are called
aps.push(ap)
end
end
}
end
ary_threads.each { |th| th.join }
break if jobs.size == 0
end
错误是
/usr/lib/ruby/1.9.1/net/http.rb:762:in `initialize': getaddrinfo: Name or service not known (SocketError)
from /usr/lib/ruby/1.9.1/net/http.rb:762:in `open'
from /usr/lib/ruby/1.9.1/net/http.rb:762:in `block in connect'
from /usr/lib/ruby/1.9.1/timeout.rb:54:in `timeout'
from /usr/lib/ruby/1.9.1/timeout.rb:99:in `timeout'
from /usr/lib/ruby/1.9.1/net/http.rb:762:in `connect'
from /usr/lib/ruby/1.9.1/net/http.rb:755:in `do_start'
from /usr/lib/ruby/1.9.1/net/http.rb:744:in `start'
from /usr/lib/ruby/1.9.1/open-uri.rb:306:in `open_http'
from /usr/lib/ruby/1.9.1/open-uri.rb:775:in `buffer_open'
from /usr/lib/ruby/1.9.1/open-uri.rb:203:in `block in open_loop'
from /usr/lib/ruby/1.9.1/open-uri.rb:201:in `catch'
from /usr/lib/ruby/1.9.1/open-uri.rb:201:in `open_loop'
from /usr/lib/ruby/1.9.1/open-uri.rb:146:in `open_uri'
from /var/lib/gems/1.9.1/gems/open-uri-cached-0.0.5/lib/open-uri/cached.rb:10:in `open_uri'
from /usr/lib/ruby/1.9.1/open-uri.rb:677:in `open'
from /usr/lib/ruby/1.9.1/open-uri.rb:33:in `open'
from Test1.rb:42:in `request'
from Test1.rb:77:in `block (3 levels) in <main>'
为什么会这样?有没有人遇到过类似的问题?请帮我!
第一个问题后3小时,我找到了临时解决方案。如果我将“请求”方法中的“打开”方法与“开始~救援~重试~结束”夹在中间,则第二次“打开”调用时不会发生错误。这是代码。
begin
response = open(url)
rescue Exception
puts url
puts "retrying"
retry
end
捕获异常并显示 url 和“重试”后,将永远不会显示 url 和“重试”并且程序正常工作:) 但我仍然找不到导致此问题的原因。