behavior of run_on_wait in Parallel::ForkManager

C

ctcgag

Parallel::ForkManager allows one to set a code-ref to be run when the
parent program has to wait for children to exit (because the limit on
number of active children has been met or exceeded). One can set a timer
to determine how often the callback is invoked during extended waiting.
However, I do not want it to wait unnecessarily, i.e. the waiting period
should apply only to another callback, and not to other events which could
allow the main program to proceed, like the exit of one of the children it
is waiting on.

The module code implementing this is:

sub on_wait { my ($s)=@_;
if(ref($s->{on_wait}) eq 'CODE') {
$s->{on_wait}->();
if (defined $s->{on_wait_period}) {
local $SIG{CHLD} = sub { } if ! defined $SIG{CHLD};
select undef, undef, undef, $s->{on_wait_period}
};
};
};

What I am observing (on Linux, under both Perl 5.6.1 and 5.8.0) is that
sometimes the receipt of a SIGCHLD will interupt the "select", and then
allow the program to go on it's way without finishing the time (The desired
result); while sometimes the receipt of a SIGCHLD will apparently either
not interupt the "select" at all, or will interupt it but them restart it.
(not the desired result.)

As best as I can tell from experimentation, the desired behavior happens
only the first time the on_wait method is invoked, and the undesired
behavior happens thereafter, although I am far from certain about that.

Is there a simple change I can make so that I always get the desired
behavior?

Experiments done with:

use strict;
use warnings;
use Parallel::ForkManager;

my $w=10;
my $s=5;

my $pm=Parallel::ForkManager->new(2);
$pm->run_on_wait(sub {warn "Waiting Patiently. ", scalar time} , $w);
foreach (1..5) {
$pm->start() and next;
warn "Started event $_. ", scalar time;
sleep $s;
warn "Ending event $_. ", scalar time;
$pm->finish();
};
$pm->run_on_wait(sub {warn "Waiting Patiently to END. ", scalar time} ,
$w); $pm->wait_all_children;



Thanks,

Xho
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top