0

Does additional code in the :content_cb-callback slow down the download?

Supposed the additional code would take 1_000/1_000_000 seconds to run and the callback gets called 1_000 times, would that slow down the download for 1_000/1_000_000 * 1_000 seconds?

#!/usr/bin/env perl
use warnings;
use 5.012;
use Time::HiRes qw(usleep);
use File::Basename;
use LWP::UserAgent;

my $url = 'my_url';
my $file_name = basename $url;
my $ua = LWP::UserAgent->new();

open my $fh, '>>:raw', $file_name or die $!;
my $res = $ua->get( 
    $url,
    ':content_cb' => sub { 
        my ( $chunk, $res, $proto ) = @_;
        print $fh $chunk; 
        usleep( 1000 ); # code substitute
    },
);
close $fh;
sid_com
  • 24,137
  • 26
  • 96
  • 187

1 Answers1

2

I would recommend to use HTTP::Async module.

use HTTP::Async;

my $url = 'http://...';
my $async = HTTP::Async->new;

$async->add( HTTP::Request->new( GET => $url ) );

while ( my $response = $async->wait_for_next_response ) {
  # Do some processing with $response
}
Ashley
  • 4,307
  • 2
  • 19
  • 28
Ωmega
  • 42,614
  • 34
  • 134
  • 203