Jim
Jim

Reputation: 19582

Reuse LWP:UserAgent?

How do you optimally use LWP::UserAgent in perl?
If I need to make several http calls would I reuse the same object?
E.g.

my $browser = LWP::UserAgent->new;    
foreach my $url (@urls) {  
   my $response = $browser->get( $url );  
   # process response  
}

Or

foreach my $url (@urls) {  
   my $browser = LWP::UserAgent->new;    
   my $response = $browser->get( $url );  
   # process response  
}

It seems to me that the second version is inefficient as it would reopen the connection each time right? Any issues I should be aware of?

Upvotes: 2

Views: 1847

Answers (1)

ThisSuitIsBlackNot
ThisSuitIsBlackNot

Reputation: 24083

As Miller commented, your two code examples are not tremendously different in terms of efficiency. Both will send a new GET request over a new connection for each loop iteration, which is more significant than the cost of creating an object.

You can cache connections using the experimental module LWP::ConnCache:

use LWP::ConnCache;
use LWP::UserAgent;

my $cache = LWP::ConnCache->new;
$cache->total_capacity(10); # Cache up to 10 connections

my $ua = LWP::UserAgent->new(conn_cache => $cache);
# Alternatively, my $ua = LWP::UserAgent->new(keep_alive => 10);

$ua->get('http://www.google.com');
$ua->get('http://www.google.com'); # Should reuse cached connection

Note that you can use the same LWP::ConnCache object in multiple LWP::UserAgents.

Upvotes: 2

Related Questions