- 论坛徽章:
- 1
|
- #!/usr/bin/perl
- use strict;
- use warnings;
- use Smart::Comments;
- use Coro;
- use AnyEvent::HTTP;
- use Benchmark;
- my $T0=Benchmark->new;
- $Coro::POOL_SIZE = 2;
- my $count = 1000;
- my $limit = Coro::Semaphore->new( $Coro::POOL_SIZE );
- my $sem = Coro::Semaphore->new( 1 - $count);
- for( 1 .. $count ){
- $limit->down;
- async_pool {
- print "$_[0] start...\n";
- http_get 'http://www.baidu.com', cb => Coro::rouse_cb;
- my @res = Coro::rouse_wait;
- print $_[0]."done! $res[1]->{Status}\n";
- $sem->up;
- $limit->up; } $_;
- };
- $sem->down;
- my $T1=Benchmark->new;
- my $td = Benchmark::timediff($T1, $T0);
- print Benchmark::timestr($td)."\n";
复制代码 这种模式好理解,就不知道斑竹是否还有更高效的代码? |
|