in reply to IO::Select - is it right for this?

can't you simply use the open on "-|" in order to read output from your child?
while ($more_childs_to_produce) { $pid= open($child[$childnumber++], "-|"); if ($pid) { # parent # here we have another "file" # to read frome with <$child[$childnumber-1]> } else { # child # Do the child's work and exit; } }
Taken from perldoc perlipc

Replies are listed 'Best First'.
Re: Re: IO::Select - is it right for this?
by mcogan1966 (Monk) on Oct 20, 2003 at 13:42 UTC
    for ($i=0; $i<`#of children to do`; $i++;) { $pid[$i]= open($created_file_handle, "$custom_call_statement -|"); if ($pid[$i]) { waitpid($pid[$i], 0); } # parent waits for child to + finish } else { while (<$created_file_handle>) { $out[$i] .= $_; } # add outpu +t to the array exit; } }
    Problem is, and it's the same when forking, that the parent can't see @out. Should I move the $out[$i] .= $_; to the parent process? The forked processes work fine, and fairly quickly, but I need to get the child to tell the parent what it found.

    I'd rather not have to use the temp file solution, as it will get very messy when having multiple users run multiple processes at the same time. That's going to make this whole process run a lot slower. I'd rather see if I can find a solution that passes data faster back to the main process.
      To be honest... I really don't understand your code above. Sorry...
      But then... Here is a complete program that might help you. It forks 2 children and collects the children's output in an array.
      Be aware that there is no error handling (can't fork etc.) yet.
      use warnings; use strict; my $children= 2; # How many to fork my @out; my $first_child; $|=1; # unbuffered output to see the effect my $pid= open($first_child, '-|'); if ($pid) { # parent: Collects all output @out= <$first_child>; } else { # child forks again and produces output my(@child, @childpid); for (my $i=0; $i<$children; ++$i) { $childpid[$i]= open($child[$i], '|-'); if ($childpid[$i]) { # parent # does nothing yet } else { # child produces some output my $j=10; while ($j--) { print "I'm child $i and will tell this $j more time(s) +\n"; sleep 1; } exit; } } foreach (@child) { # closing all child handles close $_; } exit; } print @out;
        Actually, I've found a solution that works.
        I'll post it here later in the day, when I have some time to copy it from the other machine(no, they are not connected in any way).

        But thank you for the look.
        And for my next trick, I'm going to turn it into a duck, or something that uses shared memeory, whichever makes me laugh more.