mrmaps mrmaps - 1 year ago 72
Perl Question

Why is my code using Parallel::ForkManager producing zombies?

A piece of code I'm writing uses Parallel::ForkManager and I'm noticing it is creating many zombies as the script runs. I'm wondering if I'm missing something when it comes to the


my $fork_manager = new Parallel::ForkManager->($ENV{CPUS})

for(my $i = 0; $i < scalar @plates; $i++){
my $offset = get_full_plate_offsets(@{$plates[$i]});
make_path(File::Spec->catfile($tmp_dir, $i));
foreach my $cell (keys %{$offset}){
my($x, $y) = @{$offset->{$cell}};
$fork_manager->start("$cell @ $x, $y") and next;
my $out_file = File::Spec->catfile($tmp_dir, $i, "$cell.jpg");
my $out_text = File::Spec->catfile($tmp_dir, $i, "$cell.txt");
split_file($input_jpg, [$x, $y], $out_file);
my $result = do_something($out_file);
open(my $FH, '>', $out_text);
print $FH "$result\n";

Also a clarifying question. Zombies are always bad right?

At first I was under the impression that zombie processes are just processes that haven't been recovered by their parent yet. Now I'm wondering if my code is not waiting on the children at all.

Answer Source

P::FM only reaps when start is called and the maximum number of children are running, and when wait_children or wait_all_children are called. Children that exit become zombies until then.

You will never have more children (including zombies) than specified, so the temporary presence of zombies isn't a bad thing[1]. The only catch is that the run_on_finish handler will only be executed when the child process is reaped, so this indicates that it's not being called as soon as possible.

I believe you can use the following to reap children sooner:

$SIG{CHLD} = sub { $pm->wait_children };

  1. See Is a persistent zombie process sign of a bug? for a general answer.