Hi, I am using Parallel::ForkManager to spawn several child processes to run an external command and return the exit code from the children back to the parent to enter into a DB. I am able to print out the exit code by using run_on_finish callback, but I haven't figured out how to process all the exit codes returned by the children, from the parent. The documentation states that the run_on_finish() is called in the parent process, but it seems like there is no way to access any of the variables of the parent. (Unless I am doing something wrong.) Eventually, I'd like to insert all the exit codes from the children into the mysql db connection from the parent.

What's the best way to do this? I've considered declaring a hash from the parent and add the key-value pairs from run_on_finish() callback, but it looks like the only parameters that are passed into run_on_finish() are: pid, exit_code, ident, exit_signal, and core

Am I barking up the wrong tree here? Should I explore using IPC::Sharable or pipes to communicate between the parent and the children?

Thanks in advance, and here's the code snippet:

my $MAX_PROCESSES = 120; my $pm = new Parallel::ForkManager($MAX_PROCESSES); $pm->run_on_finish( sub { my ($pid, $exit_code, $ident) = @_; print "run_on_finish: $ident (pid: $pid) exited " . "with +code: [$exit_code]\n"; somehow_pass_exit_code_and_ident_to_parent_for_processing(); + } ); $pm->run_on_start( sub { my ($pid,$ident)=@_; print "** $ident started, pid: $pid\n"; } ); for(my $i=0; $i< @hosts; $i++){ $pm->start($hosts[$i]) and next; system(@some_command); $return_code = $? >> 8; $pm->finish($return_code); }

In reply to Parallel::ForkManager run_on_finish exit code handler by sojourn548

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.