Skip to content

Instantly share code, notes, and snippets.

@Mons
Created Feb 13, 2015
Embed
What would you like to do?
#!/usr/bin/env perl
use EV;
use Scalar::Util 'weaken';
my @tasks = qw(1 2 3);
sub dbg(@) {
printf "[%0.06f] %s\n",EV::now(), "@_";
}
my $every = 0.5;
my $i = -1;
my $loop;$loop = sub {
my $ix = ++$i;
return if $ix > $#tasks;
dbg("start job $ix");
my $start = EV::now;
my $j;$j = EV::timer rand(0.4),0, sub { undef $j;
my $wait = $every - (EV::now - $start);
dbg "Done job $ix, wait left = $wait";
$wait = 0 if $wait < 0;
my $w;$w = EV::timer $wait,0, sub { undef $w;
$loop->();
}
};
};$loop->();weaken($loop);
EV::loop;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment