Created
March 18, 2014 12:06
-
-
Save Mons/9618798 to your computer and use it in GitHub Desktop.
AnyEvent loop
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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