gpt4 book ai didi

ruby - 在多个线程中运行时出现 "Name or service not known (SocketError)"错误

转载 作者:数据小太阳 更新时间:2023-10-29 07:17:51 26 4
gpt4 key购买 nike

我制作了一个并行解析文本文件和下载数据的程序。在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 和“重试”,程序将正常运行:)但我仍然找不到导致此问题的原因。

最佳答案

我认为这可能是因为线程之间存在竞争条件。尝试以原子方式执行操作。放置互斥锁。

    @mutex = Mutex.new

@mutex.syncronize do
...

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

关于ruby - 在多个线程中运行时出现 "Name or service not known (SocketError)"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15631678/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com