How can I get the value from the child process?

I have a script and in some part I start some processes to complete the task and the main process is waiting for all children to finish.
So far so good.
Problem: I am interested in getting the maximum time taken by each child process while working on what it needed to do.
Now I just look at the logs where I print the time spent on every action the child process was doing and try to find more or less the time. I know the only way to get something from the child process is through some kind of shared memory, but I was wondering if there is a "out-of-the-box" / simple solution? I mean to return the time and the parent process prints them nicely in one place.
I thought there might be a better way than just checking all the logs ...

Update based on comments:
I am not interested in the times of the child processes, i.e. which child takes longer to finish. Each child process works on tasks X. Each of the tasks ends up in the worst case. I'm looking to find Y ie, the exact time it took for the child process to complete one of X's tasks.

+3


source to share


1 answer


The biggest limitation fork()

is that it doesn't make IPC as easy as threads

. Besides capturing when a process starts and exits, what you do differently has a whole segment of perl documentation.

I would guess that what you probably want is pipe

and connect it to the child.



Something like this (not tested yet, I'm on a windows box!)

use strict;
use warnings;

use Parallel::ForkManager;

my $manager = Parallel::ForkManager -> new ( 5 ) ; 
pipe ( my $read_handle, my $write_handle );

for ( 1..10 ) {
    $manager -> start and next; 
    close ( $read_handle ); 
    print {$write_handle} "$$ - child says hello!\n";
    $manager -> finish; 
}
close ( $write_handle ); 

while ( <$read_handle> ) { print; }

$manager -> wait_all_children();

      

+1


source







All Articles