develooper Front page | perl.libwww | Postings from March 2002


Thread Next
Sean M. Burke
March 19, 2002 02:28
Message ID:
I use LWP mainly on my laptop, which accesses the Internet across slow 
dialup.  And I was thinking today how I wanted a LWP::UserAgent object 
which was sort of like LWP::RobotUA in that it delayed between requests so 
that it wouldn't be using too much of my bandwidth -- except behaving a bit 
So after some pondering, I made an LWP subclass that does just that -- it's 
asleep (about) half the time, hence its name: LWP::HalfSleepyUA.  And here 
it is!
Should I release this to CPAN?  It's trivial, but who knows, it may be 
useful to someone:

require 5;
package LWP::HalfSleepyUA;
use LWP ();
use LWP::Debug ();
use LWP::UserAgent ();
@ISA = ('LWP::UserAgent');
use strict;

sub simple_request {
   my $this = shift;
   if(defined( $this->{'_not_until'} )) {
     my $sleep = $this->{'_not_until'} - time();
     if($sleep > 0) {
       LWP::Debug::debug("Sleeping for $sleep seconds.");

   my $start_time = time();
   my $resp = $this->SUPER::simple_request(@_);
   $this->{'_not_until'} = time() + (time() - $start_time) + 1;
    # We could be clever and do 2*time() - $start_time + 1, but
    #  that could cause us to stop dealing with INTs.

   # Well, we shouldn't really bother sleeping if the request is
   # to a data: URL, but that just means less sleeping /next/
   # time!  Funny how it all works out right that way.

   LWP::Debug::debug('No further requests until T='
     . $this->{'_not_until'} . 's!');

   return $resp;


Sean M. Burke

Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About