und3f / anyevent-ping

Asynchronous ping with AnyEvent
2 stars 4 forks source link

Callback after each ping #5

Open und3f opened 10 years ago

und3f commented 10 years ago

Is it possible to get a callback after every ping? Currently as I see the sub is only called after all 3 have finished so the only way I could get what I wanted was calling ping 3 time as in this code:

use AnyEvent;
use AnyEvent::Ping;
my $c = AnyEvent->condvar;
my $ping = AnyEvent::Ping->new;

my $n = 3;
my $host = shift or die "Usage: $0 hostname\n";

sub process {
    my $result = shift;
    print "Result: ", $result->[0][0], " in ", $result->[0][1], " seconds\n";

    $n--;
    if ($n > 0) {
        $ping->ping($host, 1, \&process);
    } else {
        $c->send;
    }
}

$ping->ping($host, 1, \&process);

$c->recv;
$ping->end;

regards Gabor