Skip to content

Instantly share code, notes, and snippets.

@zigorou
Created November 8, 2011 17:11
Show Gist options
  • Select an option

  • Save zigorou/1348402 to your computer and use it in GitHub Desktop.

Select an option

Save zigorou/1348402 to your computer and use it in GitHub Desktop.
AnyEvent Timer and Idle Watcher
#!/usr/bin/env perl
use strict;
use warnings;
use AnyEvent;
my $cv = AnyEvent->condvar;
for my $i (1..10) {
$cv->begin;
my $after = int(rand(2)) + 1;
warn "[begin] times: $i. after: $after";
my $timer; $timer = AnyEvent->timer(
after => $after,
cb => sub {
warn "[end] times: $i. after: $after";
undef $timer;
$cv->end;
}
);
my $idle; $idle = AnyEvent->idle(
cb => sub {
if ($timer) {
warn "[idle] times: $i. after: $after";
}
else {
undef $idle;
}
}
);
}
$cv->recv;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment