I fear your version does not work in parallel!
I just tested it with this variant:
$g=10; $|=1; for ($i=0; $i<$g; $i++) { pipe ($rh, $wh); # both of these are generated in the loop if ($pid[$i]=fork()) { # parent process waitpid($pid[$i], 0); # wait for child close ($wh); while (<$rh>) { # gimme the output if ($_ =~ m/^Error/) { push (@error, $_); } elsif ($_ =~ m/^Hits/) { push (@hits, $_); } else { push (@data, $_); } # each is a result from an engine } } else { close ($rh); open (STDOUT, ">&$wh"); $|=1; print "I will wait for ",$i+2," seconds\n"; sleep $i+1; print "Good morning. I slept about ",$i+2," seconds\n"; exit(0); } } print @data;
Two problems:
  1. It really doesn't work in parallel
  2. It doesn't give the output to the parent
It produces this output:
I will wait for 2 seconds Good morning. I slept about 2 seconds I will wait for 3 seconds Good morning. I slept about 3 seconds I will wait for 4 seconds Good morning. I slept about 4 seconds I will wait for 5 seconds Good morning. I slept about 5 seconds I will wait for 6 seconds Good morning. I slept about 6 seconds I will wait for 7 seconds Good morning. I slept about 7 seconds I will wait for 8 seconds Good morning. I slept about 8 seconds I will wait for 9 seconds Good morning. I slept about 9 seconds I will wait for 10 seconds Good morning. I slept about 10 seconds I will wait for 11 seconds Good morning. I slept about 11 seconds
But the output should be something like this:
I will wait for 2 seconds I will wait for 3 seconds I will wait for 4 seconds I will wait for 5 seconds I will wait for 6 seconds I will wait for 7 seconds I will wait for 8 seconds I will wait for 9 seconds I will wait for 10 seconds I will wait for 11 seconds Good morning. I slept about 2 seconds Good morning. I slept about 3 seconds Good morning. I slept about 4 seconds Good morning. I slept about 5 seconds Good morning. I slept about 6 seconds Good morning. I slept about 7 seconds Good morning. I slept about 8 seconds Good morning. I slept about 9 seconds Good morning. I slept about 10 seconds Good morning. I slept about 11 seconds
which I produced with a variant of my former version:
#!/usr/local/bin/perl use warnings; use strict; my $children= 10; my @out; my $first_child; $|=1; my $pid= open($first_child, '-|'); if ($pid) { # parent @out= <$first_child>; } else { my(@child, @childpid); for (my $i=0; $i<$children; ++$i) { $childpid[$i]= open($child[$i], '|-'); if ($childpid[$i]) { # do nothing yet } else { print "I will wait for ",$i+2," seconds\n"; sleep $i+1; print "Good morning. I slept about ",$i+2," seconds\n"; exit; } } foreach (@child) { close $_; } exit; } print @out;

In reply to Re: Re: IO::Select - is it right for this? by Skeeve
in thread IO::Select - is it right for this? by mcogan1966

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.