in reply to Get Timout with LWP
LWP::Simple is just that. Simple. It will hang if it can open a socket to a server but the server then plays dead and does not respond. LWP::UserAgent is much more sophisticated with its timeouts and will return if it times out.......
BUT THERE IS A CAVEAT: Because of the way it is implemented in LWP::UserAgent the timeout is best termed as *fuzzy*. It will timeout but if you have a connection to a (say) overloaded server that trickle feeds data back to your client application it may take orders of magnitude longer to timeout than the number you specify. If you absolutely need a hard limit use alarm.
Here is an example. Although the timeout is set to 2 seconds LWP will wait 30 seconds to get the trickle data. Effectively what the timeout means to LWP is 1) make the connection within TIMEOUT seconds and 2) get at least one byte of data from a non blocking socket every TIMEOUT seconds. It will return if either of these are not fulfilled but as you can see you can still kinda hang it.
As an aside it you are scraping sites, and they run anti-scraping response throttling this scenario if quite realistic.
# this is the test server that does a tricke feed response #!/usr/bin/perl use IO::Socket; use IO::Select; $lsn = IO::Socket::INET->new( Listen => 1, LocalAddr => 'localhost', LocalPort => 9000,); my $client = new IO::Select( $lsn ); while( my @ready = $client->can_read ) { for my $fh (@ready) { if($fh == $lsn) { warn "Accepted new socket\n"; my $new = $lsn->accept; $client->add($new); } else { # Process socket warn "Getting data\n"; $data = <$fh>; # yeah yeah this is only a toy app warn "Got $data\nDoing stuff slowly!\n"; my @response = split '', "HTTP/1.1 200 OK\n\nHello World!\ +n"; for( @response) { warn $_; sleep 1; print {$fh} $_; } $client->remove($fh); $fh->close(); } } } # this is the test client #!/usr/bin/perl use LWP::UserAgent; use Data::Dumper; my $ua = LWP::UserAgent->new( timeout => 2 ); $response = $ua->get('http://localhost:9000/'); print Dumper $response;
cheers
tachyon
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Get Timout with LWP
by IlyaM (Parson) on Jun 21, 2004 at 15:54 UTC | |
by tachyon (Chancellor) on Jun 22, 2004 at 01:45 UTC | |
by tachyon (Chancellor) on Jun 22, 2004 at 02:47 UTC |