简体   繁体   English

如何在没有响应的网站上等待Mechanize?

[英]What can I do about Mechanize waiting on an unresponsive web site?

I noticed that when I fetch a site that is not responding using Mechanize , it just keeps on waiting. 我注意到当我获取一个没有使用Mechanize响应的网站时,它只是在等待。

How can I overcome this problem? 我怎样才能克服这个问题?

There's a couple ways to deal with it. 有几种方法可以解决它。

Open-Uri, and Net::HTTP have ways of passing in timeout values, which then tell the underlying networking stack how long you are willing to wait. Open-Uri和Net :: HTTP都有传递超时值的方法,然后告诉底层网络堆栈你愿意等多久。 For instance, Mechanize lets you get at its settings when you initialize an instance, something like: 例如,Mechanize允许您在初始化实例时获取其设置,例如:

mech = Mechanize.new { |agent|
  agent.open_timeout   = 5
  agent.read_timeout   = 5
}

It's all in the docs for new but you'll have to view the source to see what instance variables you can get at. 这一切都在new文档中,但您必须查看源代码以查看可以获得的实例变量。

Or you can use Ruby's timeout module: 或者你可以使用Ruby的timeout模块:

require 'timeout'
status = Timeout::timeout(5) {
  # Something that should be interrupted if it takes too much time...
}

http://mechanize.rubyforge.org/mechanize/Mechanize.html on this page there are 2 undocumented attributes open_timeout and read_timeout , try using them. http://mechanize.rubyforge.org/mechanize/Mechanize.html此页面上有2个无证属性open_timeoutread_timeout ,请尝试使用它们。

agent = Mechanize.new { |a| a.log = Logger.new("mech.log") }
agent.keep_alive=false
agent.open_timeout=15
agent.read_timeout=15

HTH HTH

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM