What can I do to Mechanize Standby on a non-responsive website?

I noticed that when I get a site that is not responding with Mechanize , it just keeps waiting.

How can I solve this problem?

+4
source share
2 answers

There are several ways to handle this.

Open-Uri and Net :: HTTP have timeout methods that then tell the underlying network stack how much time you are ready to wait. For example, Mechanize allows you to get your settings when you initialize an instance, for example:

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

Everything is in the docs for new , but you will need to look at the source to see which instance variables you can get.

Or you can use the Ruby timeout module:

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

http://mechanize.rubyforge.org/mechanize/Mechanize.html on this page there are 2 undocumented attributes open_timeout and read_timeout , try to use them.

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

NTN

+3
source

Source: https://habr.com/ru/post/1347106/


All Articles