new_perl
new_perl

Reputation: 7735

How to make LWP to just continue instead of terminating when timeout happens?

read timeout at /usr/lib/perl5/site_perl/5.8.8/LWP/Protocol/http.pm line 426. at /usr/lib/perl5/site_perl/5.8.8/LWP/UserAgent.pm line 844

Anyone knows?

Upvotes: 1

Views: 2976

Answers (3)

calvillo
calvillo

Reputation: 882

Sometimes, when the request is sent and the response is being received, and then, the connection is interrupted in the middle of this process (your Internet connection goes down), LWP will return a $response->code of 200, and will have a $response->content equal to a partial download of the content you requested. Then, if you retry, you will immediately get the read timeout error.

Of course, this only happens if the connection is lost at a very specific part of the process... but it happens.

I suggest that you verify if the content you received is complete and, if it's not, work around it before attempting your next connection.

Upvotes: 0

ysth
ysth

Reputation: 98398

Just continue what?

If you want to disable timing out, just call ->timeout(0) on your user agent object.

To prevent the exception from ending your process, wrap the code in an eval {} block; see http://perldoc.perl.org/functions/eval.html.

But I'm curious to know how you are getting the exception you show; using LWP in the normal way will already catch that exception and return an error response, allowing your code to continue. Show your code if you want help.

Upvotes: 1

ikegami
ikegami

Reputation: 385789

LWP does continue on with the next statement on timeout. Specifically, it returns an HTTP::Response object with a 5xx error code.

>perl -MLWP -e"my $ua = LWP::UserAgent->new(timeout => 1); print $ua->get('http://...something really slow...')->as_string;"
500 read timeout
Content-Type: text/plain
Client-Date: Thu, 30 Jun 2011 06:35:11 GMT
Client-Warning: Internal response

read timeout at .../lib/LWP/Protocol/http.pm line 433.

Upvotes: 2

Related Questions