T'is easy with threads:
Updated: Changed < to <= and made final loop wait for all kids to finish.
#! perl -slw use strict; use threads qw[ yield ]; use threads::shared; our $MAXTIME ||= 7200; our $CMDS ||= 140; our $CONCURRENT ||= 6; my $running :shared = 0; my @cmds = map { my $time = int rand $MAXTIME; qq[ perl -e"sleep $time" ]; } 1 .. $CMDS; for my $cmd ( @cmds ) { sleep 1 while do{ lock $running; $running >= $CONCURRENT }; async{ my $n = do{ lock $running; ++$running; }; my $tid = threads->tid; print "$tid: starting '$cmd' ($n)"; system $cmd; print "$tid: ending"; { lock $running; --$running; } }->detach; yield; } sleep 1 while do{ lock $running; $running };
Sample run:
In reply to Re: Using perl to manage child processes - critique my code.
by BrowserUk
in thread Using perl to manage child processes - critique my code.
by FatDog
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |