cheshirecat has asked for the wisdom of the Perl Monks concerning the following question:
I'm having major problems with LWP and timeouts.
As it stands, my cgi script is making a request of a 3rd party site which occasionally takes more than 10 seconds to fufill.
What I want to do is cancel the request if it takes longer than 3 seconds (it usually completes in this time but sometimes not).
Here's a code snippet, any comments welcome
Cheers Cheshire Catmy $timeout = 3 my $timedout = 0; my $oops = 0; my $error; my $stime; my $etime; eval { local $SIG{ALRM} = sub { die "timeout\n" }; # die out of eval + statement not entire program alarm $timeout; $stime = time; # make a note of the time now $result = $ua->request($req); # make request of website $etime = time; # make a note of the time now alarm 0; }; if ($@) { if ($@ eq "timeout\n") { $timedout = 1; } if ($@ and $@ ne "timeout\n") { $oops = 1; } # if there was an erro +r in the eval and it wasn't the alarm } $error = $timedout || $oops; my $dtime = $etime - $stime; # figure out the time taken for the requ +est if (!$error) { open(TEST,">/var/tmp/t4.txt"); print TEST "http result\n"; # here I get told that the request took 10 seconds and my time out is +3 seconds print TEST $result->status_line." : $dtime secs taken, timeout = $ti +meout\n"; # print TEST "ok\n"; close(TEST); } else { open(TEST,">/var/tmp/t4.txt"); if ($oops) { print TEST "oops error occurred: $@\n"; } else { print TEST "timeout\n";} close(TEST); }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Problems trying to timeout LWP requests
by rob_au (Abbot) on Feb 06, 2002 at 12:25 UTC | |
by tomhukins (Curate) on Feb 06, 2002 at 12:43 UTC | |
by Anonymous Monk on Feb 06, 2002 at 14:32 UTC | |
by cheshirecat (Sexton) on Feb 06, 2002 at 12:47 UTC | |
|
Re: Problems trying to timeout LWP requests
by cheshirecat (Sexton) on Feb 06, 2002 at 12:18 UTC |