I am trying to create a perl daemon that sits and runs non-stop.
I want it to create 20 child processes at a time. When one finishes, it should die and spawn another.

Here is the code I have now:

... use constant NUM_CHILDREN => 20; my $num_children = 0; my $child_index = 0; my $pm = Parallel::ForkManager->new(NUM_CHILDREN); $pm->run_on_start( sub { my ($pid,$ident)=@_; write_file(DIR."/daemon_${ident}.pid", $pid); $num_children++; msg("$ident) Starting $pid"); }); $pm->run_on_finish( sub { my ($pid, $exit_code, $ident, $exit_signal, $core_dump, $job) = @_ +; $num_children--; msg("${ident}) Process completed: @_"); if ($daemon->{'identity'}{'status'} > 0) { startChild($ident); } }); # reaper subroutine sub REAP { while (1) { my $id = waitpid( -1, WNOHANG); if ($id == -1) { return; } if ($id > 0) { msg("JUST REAPED $id"); } } $SIG{CHLD} = \&REAP; } $SIG{CHLD} = \&REAP; # start all the child processes startChildren(); sub startChildren { for (1..NUM_CHILDREN) { startChild(); } $pm->wait_all_child; msg('All children are done.'); } sub startChild { my $child_id = 'child_'.++$child_index; my $job = shift(@{$daemon->{jobs}}); $job->{process_id} = $child_id; $pm->start($child_id) and next; process($job); $pm->finish(0, $job); } ...

Things to note:
- the work in process() IS getting completed.
- all processes are getting REAPed,
- but none are ever getting to the $pm->run_on_finish() method.
- I never get the 'All children are done.' message.
- If I do a log inside $pm->run_on_wait() i get a BUNCH of of entries.

What am I doing wrong that prevents these processes from reaching run_on_finish() ?

If things are indeed running the way they should be, then what do I need to do to make this work the way I want? What I want: start 20 child processes, have each of them die in due course, and immediately have a new one start up, via the startChild() function. Each child may be dealing with a fair amount of data, which is why I want them to die, so as to free up that memory.

I do not want to use a while(1) {} loop in the master section of the code as that tends to spike the CPU at times.

Any feedback would be most welcome.


In reply to Parallel::ForkManager never reaching run_on_finish() by swafo

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.