Hi all,

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

my $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); }
Cheers Cheshire Cat

In reply to Problems trying to timeout LWP requests by cheshirecat

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.