gpt4 book ai didi

ruby - ConnectionPool::Error(没有检查出连接)

转载 作者:太空宇宙 更新时间:2023-11-03 18:04:35 24 4
gpt4 key购买 nike

我在尝试使用 mechanize 抓取网站时遇到此错误。

这是我的代码:

agent = Mechanize.new

agent.user_agent_alias = 'Mac Safari'

agent.keep_alive = false

page = agent.get('https://web.archive.org/web/20170417084732/https://www.cs.auckland.ac.nz/~andwhay/postlist.html')

page.links_with(:text => 'post').each do |link|
post = link.click
Article.create(
user_id: 1,
title: post.css('title'),
text: post.at("//div[@itemprop = 'description']")
)
end

我还使用了 this code以避免“太多连接重置”错误。

最佳答案

链接博客文章中的代码似乎与 net-http-persistent gem 的 v3.0.0 不兼容。请注意,Mechanize v2.7.6(撰写本文时的当前版本)与 net-http-persistent >= v2.5.2 兼容,其中包括 v3.0.0。

简短的回答是执行以下操作之一:

  • 将 net-http-persistent 固定到 v2.9.4
  • (实验性) 删除链接博文第 44 行对 self.http.shutdown 的调用

长答案是 net-http-persistent gem 在 v3.0.0 中开始使用 connection_pool gem,它改变了 Net::HTTP::Persistent#shutdown 的行为(又名 self.http.shutdownMechanize::HTTP::Agent 中)。如果在调用 shutdown 后发出请求,新行为会引发 ConnectionPool::Error(“未 checkout 任何连接”)。

但是,查看 net-http-persistent v2.9.4 和 v3.0.0 的代码,似乎 self.http.shutdown 一开始就不是必需的。 shutdown 的主要目的似乎是在每个连接上调用 finish。在 v2.9.4 和 v3.0.0 中,当 Net::HTTP::Persistent#requestErrno::ECONNRESET 异常中拯救时(这一切的最初原因) ,它只重试一次,然后调用 Net::HTTP::Persistent#request_failedrequest_failed 依次通过连接调用 Net::HTTP::Persistent#finish。因此,似乎唯一必要的猴子补丁就是重试不止一次。

关于ruby - ConnectionPool::Error(没有检查出连接),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51046179/

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