这个是CU上斑竹提供的例子,
使用AnyEvent::HTTP模块并行异步抓取网页
写的确实结构很清晰,可以作为经典例子
#!/usr/bin/perl
use strict;
use warnings;
use AnyEvent;
use AnyEvent::HTTP;
my $cocurrent = 100; # 并发数
my @todoList = map { "keyword" . $_ } (1 .. 1000); # 待查询的关键词
my $cv = AnyEvent->condvar;
doit() foreach 1..$cocurrent;
sub doit{
my $word = shift @todoList;
return if not defined $word;
$cv->begin;
http_get( "http://www.baidu.com/s?wd=$word", sub { done( $word, @_ ) } );
}
sub done {
my ($word, $content, $hdr) = @_;
$cv->end();
print "Search: $word\tStatus: ", $hdr->{Status}, "\n";
doit();
}
$cv->recv();