Skip to content

Instantly share code, notes, and snippets.

@Mons
Created March 18, 2014 12:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Mons/9618798 to your computer and use it in GitHub Desktop.
Save Mons/9618798 to your computer and use it in GitHub Desktop.
AnyEvent loop
#!/usr/bin/env perl
use strict;
use 5.010;
use AnyEvent;
use AnyEvent::HTTP;
use Time::HiRes 'time';
my $records = [ (1)x4e3 ];
my $i = 0;
say 0+@$records;
<<'WRONG'
$cv = AE::cv;
for (@$records) {
$cv->begin;
http_request ... sub {
$cv->end;
}
}
$cv->recv;
WRONG
;
$AnyEvent::HTTP::MAX_PER_HOST = 100;
say "Starting in parallel by $AnyEvent::HTTP::MAX_PER_HOST";
my $job;
my $cv = AE::cv;
my $st = time;
$job = sub {
if ($i < @$records) {
if ($i % 1000 == 0) {
my $d = time - $st;
printf "Processed %d in %0.2fs (%0.2f/s)\n",$i,$d,$i/$d;
}
my $record = $records->[$i++];
# process your record here
http_request
GET => 'http://localhost/',
sub {
my ($body,$headers) = @_;
# process results
# call for continuation
$job->();
}
;
} else {
# no more jobs
$cv->end;
}
};
$cv->begin;
for ( 1 .. $AnyEvent::HTTP::MAX_PER_HOST ) {
$cv->begin;
$job->();
}
$cv->end;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment