Oded Harth Oded Harth - 1 year ago 89
Ruby Question

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.

How can I overcome this problem?

Answer Source

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. For instance, Mechanize lets you get at its settings when you initialize an instance, something like:

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.

Or you can use Ruby's timeout module:

require 'timeout'
status = Timeout::timeout(5) {
  # Something that should be interrupted if it takes too much time...
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download