Skip to content

Instantly share code, notes, and snippets.

@Mons
Created February 13, 2015 16:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Mons/928ae676254cbfaf2b49 to your computer and use it in GitHub Desktop.
Save Mons/928ae676254cbfaf2b49 to your computer and use it in GitHub Desktop.
#!/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