1

How do I properly use a timeout when I attempt to make ->get(URL) requests with WWW::Mechanize::Firefox?

my $mech = WWW::Mechanize::Firefox->new(timeout => 10); does not seem to work

Bijan
  • 7,737
  • 18
  • 89
  • 149
  • 3
    [You're not the first one to wonder about this.](http://stackoverflow.com/q/22311475/176646) Unfortunately, it seems like this isn't supported by W::M::F, so you may have to implement a timeout yourself with [`alarm`](http://perldoc.perl.org/functions/alarm.html). – ThisSuitIsBlackNot Jan 07 '16 at 18:25

1 Answers1

1

It is possible to simulate this, at least to a good extent.

You can turn off synchronization for get, in which case the call should return immediately. Then poll every $sleep_time until timeout, with some test of whether the page completed. The sleep allows all those other good pages to complete, so set $sleep_time as appropriate.

my $timeout = 10; 
my $sleep_time = 1;

my $page = get($url, synchronize => 0); 

for (1..$timeout) {
    # Test some page property that will confirm that it loaded
    last if $page->title eq $expected_title;
    sleep $sleep_time;
}

There is the issue of how exactly to confirm each page, but this should provide a working timeout.

zdim
  • 64,580
  • 5
  • 52
  • 81
  • 1
    I see that this is an older post, but an idea for a simple timeout could be useful. – zdim Apr 06 '16 at 20:08