Created
February 13, 2015 16:26
-
-
Save Mons/928ae676254cbfaf2b49 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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