sojourn548 has asked for the wisdom of the Perl Monks concerning the following question:
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); }
|
|---|