dchau has asked for the wisdom of the Perl Monks concerning the following question:

Hi this is my first time posting. I was wondering if there was a way to do something based on the LWP timeout function. For example:
foreach (@url){ chomp; $site = "$_"; $time = time(); $ua->timeout(30); $request = new HTTP::Request POST => $site; $response = $ua->request($request); $return_code = $response->code([$code]); &check_status($site, $return_code, $time); }
I want to do something when the request times out. Sort of like:
if ($ua->timeout(30)){ Write to log }
is this possible? with the timeout function? Thanks in advance.

Replies are listed 'Best First'.
Re: LWP timeout
by Jouke (Curate) on Apr 02, 2001 at 17:57 UTC
    Something like this should be possible:
    $ua -> timeout(45); $SIG{'ALRM'} = sub { die ("Timeout after 45 seconds"); };
    So you first set the timeout to 45 seconds and afterwards you connect the 'ALRM' signal to a subroutine that deals with what to do with it...

    Jouke Visser, Perl 'Adept'
      You must be warned of one potential pitfall :

      The timeout may seem to NOT work, if the host you're trying to connect is unreachable
      (you may wait long minutes whatever the number of seconds you used in your timeout() function...)
      See this post for more info.


      "Only Bad Coders Badly Code In Perl" (OBC2IP)
Re: LWP timeout
by THRAK (Monk) on Apr 02, 2001 at 18:02 UTC
    You could parse $response and look for a "408 Request Timeout" from the server. If you are LWPing, this might be useful either now or in the future.

    -THRAK
    www.polarlava.com
Re: LWP timeout
by dchau (Novice) on Apr 02, 2001 at 18:49 UTC
    I tried parsing the $response that doesnt work and i tried
    $timeout = 0; foreach (@url){ chomp; $site = "$_"; $time = time(); $ua->timeout(2); $SIG{'ALRM'} = sub {$timeout =1;}; $request = new HTTP::Request POST => $site; $response = $ua->request($request); $return_code = $response->code([$code]); print "return code = $return_code\n"; print "timeout = $timeout\n"; &check_status($site, $return_code, $time);
    When i print out $timeout it remains 0 doesnt change to 1 when the request times out