dluxhu / perl-parallel-forkmanager

Parallel::ForkManager
20 stars 11 forks source link

parent process uses CPU 100% #7

Closed skaji closed 9 years ago

skaji commented 9 years ago

I'm using Parallel::ForkManager 1.15 (which is not in this repository...)

Since 1.15, _waitpid_non_blocking() may return -1. https://metacpan.org/diff/file?target=YANICK/Parallel-ForkManager-1.15/&source=YANICK/Parallel-ForkManager-1.14/ Then if we call wait_all_children in a parent process, it calls wait4(2) again and again, and uses CPU 100%.

The following script (test.pl) reproduces this issue:

#!/usr/bin/env perl
use strict;
use warnings;
use Parallel::ForkManager;

warn "-> Using Parallel::ForkManager ", Parallel::ForkManager->VERSION, "\n";
my $p = Parallel::ForkManager->new(1);
warn "-> parent pid=$$, try `top -p $$` or `top -pid $$`\n";
{
    $p->start and last;
    sleep;
    $p->finish;
}
$p->wait_all_children;
$ perl test.pl &
-> Using Parallel::ForkManager 1.15
-> parent pid=864, try `top -p 864` or `top -pid 864`

$ top -p 864
...
  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
  864 skaji     20   0   38280   6884   2280 R  99.8  0.3   0:18.24 perl

So could you revert the 1.15 change?

skaji commented 9 years ago

Oh, sorry there is v1.15 tag, I missed it. https://github.com/dluxhu/perl-parallel-forkmanager/tree/v1.15

yanick commented 9 years ago

I'm in vacation right now, but I'll try to fix the issue once I'm back (end of next week)

skaji commented 9 years ago

Thanks! I will wait.

yanick commented 9 years ago

Unless I'm mistaken, doing

$p->set_waitpid_blocking_sleep(0);

will take care of your problem.

skaji commented 9 years ago

Yes, it fixes my problem. Thank you!

yanick commented 9 years ago

Excellent. :-)