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
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