Skip to content

Instantly share code, notes, and snippets.

@earino
Created May 23, 2014 01:44
Show Gist options
  • Save earino/1a5414d12c9b0a684d5f to your computer and use it in GitHub Desktop.
Save earino/1a5414d12c9b0a684d5f to your computer and use it in GitHub Desktop.
how i just killed the children
#that title doesn't get me on a list...
use Parallel::ForkManager;
use Data::Dumper;
my @sleep = (
10,
60,
60,
60,
60,
60,
60,
60,
60,
60,
60,
60,
60,
);
# Max 30 processes for parallel download
my $pm = new Parallel::ForkManager(10);
my $pgid = "123";
setpgrp($$, $pgid);
my @pids = ();
$pm->run_on_finish( sub {
my ($pid, $exit_code, $ident, $exit_signal, $core_dump, $data) = @_;
print "** $ident just got out of the pool ".
"with PID $pid and exit code: $exit_code\n";
my $string = ${$data};
print "Value is $string";
kill "KILL", @pids;
exit;
});
foreach my $sleep (@sleep) {
if (my $pid = $pm->start) {
push (@pids, $pid);
next; # do the fork
}
setpgrp($$, $pgid);
sleep $sleep;
my $string = "slept for $sleep seconds! feel great now!";
$pm->finish(0, \$string); # do the exit in the child process
}
$pm->wait_all_children;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment