整体框架和http几乎一样,就是更换了模块。
可以比较一下和纯AnyEvent::Ping实现
有点是控制逻辑放在了协程而不是回调函数中
use Coro;
use AnyEvent::Ping;
my $cocurrent = 100; # 并发数
my @todoList = map { "192.168.6." . $_ } (1 .. 254); #
my @coro;
my $ping = AnyEvent::Ping->new;
doit() foreach 1..$cocurrent;
sub doit{
my $ip = shift @todoList;
return if not defined $ip;
push @coro, async {
$ping->ping($ip, 1, Coro::rouse_cb );
my @result = Coro::rouse_wait;
print "Result($ip): ", $result[0][0][0]," in ", $result[0][0][1], " seconds\n";
doit();
}
}
foreach (@coro) {
print "joining\n";
$_->join;
print "joined\n";
};