use 5.14.0; use EV; use AnyEvent; use AnyEvent::Util qw(run_cmd); use File::Spec; # set up my condvars my $all_done = AE::cv; $all_done->begin for 1..50; my $ten = AE::cv; # start the first 10... start_ten($ten, $all_done, $_) for 1..2; # wait for all of them to finish. $all_done->recv(); sub start_ten { my $t = shift; my $all = shift; my $one = shift; state $started = 0; # 10/100 too much for my machine... for (1..5) { $started ++; my $proc = $started; $t->begin if $one == 1; $all->end; print "Starting...$proc\n"; my $cv = run_cmd( ['sleep', rand(3)], '>' => File::Spec->devnull(), '2>' => File::Spec->devnull(), '<' => File::Spec->devnull(), ); $cv->cb( sub { my $c = shift; my $rc = $c->recv(); print "Finished $proc: $rc\n"; $t->end() }); } # when we're done, start the next ten. $t->cb( sub { my $c = shift; print "Checking next group...($started)\n"; $c->recv; $all->ready() or start_ten($t, $all, 1) }); }