I'm afraid that won't work for you.

Here's the implementation of P::FM::start (found by doing vi `perldoc -l Parallel::ForkManager`):

sub start { my ($s,$identification)=@_; die "Cannot start another process while you are in the child process +" if $s->{in_child}; while ( ( keys %{ $s->{processes} } ) >= $s->{max_proc}) { $s->on_wait; $s->wait_one_child(defined $s->{on_wait_period} ? &WNOHANG : undef +); }; $s->wait_children; if ($s->{max_proc}) { my $pid=fork(); die "Cannot fork: $!" if !defined $pid; if ($pid) { $s->{processes}->{$pid}=$identification; $s->on_start($pid,$identification); } else { $s->{in_child}=1 if !$pid; } return $pid; } else { $s->{processes}->{$$}=$identification; $s->on_start($$,$identification); return 0; # Simulating the child which returns 0 } }
If you look at the "} else {" branch, you'll see that if no processes are available (when $s->{max_proc}==0, what set_max_procs(0) does), the task is continued in the parent process.

That looks like a cool feature, actually, since you can test out your code in a single process loop before going parallel, if you like.

I think you should have your alarm sig set a flag, which you could check in your "my $data" loop:

my $stopFlag=0; $SIG{ARLM} = sub { $stopFlag=1; }; alarm($MAX_TIME); foreach my $data (@all_data) { last if $stopFlag; my $pid = $pm->start and next; # Process or call processing routines # for $data here $pm->finish; } $pm->wait_all_children; alarm(0);
Hmmm, I think that'd work... Not sure, though.
--
Mike

In reply to Re: Graceful shutdowns and Parallel::ForkManager by RMGir
in thread Graceful shutdowns and Parallel::ForkManager by atcroft

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.