Subject: | nowait parameter for start method |
I found that sometimes I don't want ForkManager to sit around waiting
for child processes to finish so that it might start another.
In these cases, I may wish to return and deal with another queue for
example, which I might submit using a different instance of PM.
It would be nice if there were a mechanism to make ->start return if
there are too many child processes to start a new one. One method might
be to include a new method, ->start_no_wait for example. But I found
that if you add a second parameter to ->start ($ident, $nowait), which
if true would return immediately with a PID, or zero or undef. The
undef would indicate that there are too many processes running; the
caller should come back later.
I found that a simple mod to ->start would do the trick. I include the
mod below:
280 sub start {
281 my ($s,$identification, $noWait)=@_;
282 die "Cannot start another process while you are in the child
process" if $s->{in_child};
283 if ($noWait) {
284 my $kid;
285 # reap as many kids as possible (otherwise they just
hang around and gum up the works)
286 do {
287 $kid = $s->wait_one_child (&WNOHANG);
288 } while $kid > 0;
289 return undef if $s->{max_proc} && ( keys %{
$s->{processes} } ) >= $s->{max_proc};
290 } else {
291 while ($s->{max_proc} && ( keys %{ $s->{processes} } )
Show quoted text
>= $s->{max_proc}) {
292 $s->on_wait;
293 $s->wait_one_child(defined $s->{on_wait_period} ?
&WNOHANG : undef);
294 };
295 $s->wait_children;
296 }
My only reservation at this point is that ->on_wait is not called as it
is in the original while loop (which remains intact near line 292
above). However, I think that it's ok in this case, as we're NOT
waiting, but rather just checking whether any children are finished and
checking to see how many processes are running.
If we fall through this last return (on line 296 above), then the next
thing to do is the usual fork.
The calling code would then need to check for undef returned, and if
found can go on about other business. If something >= 0 is returned,
then we implement the usual behavior.