HTTP-MultiGet

 view release on metacpan or  search on metacpan

lib/AnyEvent/HTTP/MultiGet.pm  view on Meta::CPAN

package AnyEvent::HTTP::MultiGet;

use Modern::Perl;
use Moo;
use MooX::Types::MooseLike::Base qw(:all);
use Data::Dumper;
use Ref::Util qw(is_plain_arrayref);
use namespace::clean;
Log::Log4perl->wrapper_register(__PACKAGE__);

BEGIN {
  extends 'HTTP::MultiGet';
}

our $VERSION=$HTTP::MultiGet::VERSION;

=head1 NAME

AnyEvent::HTTP::MultiGet - AnyEvent->condvar Control Freindly LWP Like agent

=head1 SYNOPSIS

  use Modern::Perl;
  use AnyEvent::HTTP::MultiGet;

  my $self=AnyEvent::HTTP::MultiGet->new();
  my $count=0;
  TEST_LOOP: {
    my $req=HTTP::Request->new(GET=>'http://google.com');
    my $req_b=HTTP::Request->new(GET=>'https://127.0.0.1:5888');
    my @todo=HTTP::Request->new(GET=>'http://yahoo.com');
    push @todo,HTTP::Request->new(GET=>'http://news.com');
    push @todo,HTTP::Request->new(GET=>'https://news.com');

    my $total=2 + scalar(@todo);
    my $cv=AnyEvent->condvar;

    my $code;
    $code=sub {
      my ($obj,$request,$result)=@_;
      printf 'HTTP Response code: %i'."\n",$result->code;
      ++$count;
      if(my $next=shift @todo) {
        $self->add_cb($req,$code);
        $self->run_next;
      }
      no warnings;
      $cv->send if $total==$count;
    };
    $self->add_cb($req,$code);
    $self->add_cb($req_b,$code);
    $self->run_next;
    $cv->recv;
  }

Handling Multiple large http requests at once

  use Modern::Perl;
  use AnyEvent::HTTP::MultiGet;

  my $self=AnyEvent::HTTP::MultiGet->new();
  my $chunks=0;
  my $count=0;


  my $req=HTTP::Request->new(GET=>'https://google.com');
  my $req_b=HTTP::Request->new(GET=>'https://yahoo.com');



( run in 2.878 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )