use strict;
use Parallel::ForkManager;
print "Content-Type: text/html\n\n";
my $max_procs = 5;
my @names = qw( Fred Jim Lily Steve );
# hash to resolve PID's back to child specific information
my $pm = new Parallel::ForkManager($max_procs);
# Setup a callback for when a child finishes up so we can
# get it's exit code
$pm->on_finish(
sub { my ($pid, $exit_code, $ident) = @_;
print "** $ident just got out of the pool
".
"with PID $pid and exit code: $exit_code
\n";
}
);
$pm->on_start(
sub {
my ($pid,$ident)=@_;
print "** $ident started, pid: $pid
\n";
}
);
$pm->on_wait(
sub {
print "** Have to wait for one children ...
\n"
}
);
foreach my $child (0 .. $#names ) {
my $pid = $pm->start($names[$child]) and next;
# This code is the child process
print "This is $names[$child], Child number $child
\n";
# sleep ( 2 * $child );
print "$names[$child], Child $child is about to get out...
\n";
sleep 1;
# pass an exit code to finish
$pm->finish($child);
}
print "Waiting for Children...
\n";
$pm->wait_all_childs;
print "Everybody is out of the pool!
\n";