簡體   English   中英

如何在沒有響應的網站上等待Mechanize?

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

我注意到當我獲取一個沒有使用Mechanize響應的網站時,它只是在等待。

我怎樣才能克服這個問題?

有幾種方法可以解決它。

Open-Uri和Net :: HTTP都有傳遞超時值的方法,然后告訴底層網絡堆棧你願意等多久。 例如,Mechanize允許您在初始化實例時獲取其設置,例如:

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

這一切都在new文檔中,但您必須查看源代碼以查看可以獲得的實例變量。

或者你可以使用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此頁面上有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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM