Go for Event!

It's ugly, unfactor'd, but works.

use Event; use IO::File; my $f = IO::File->new('for t in 1 2 3 4;do echo one $t; sleep 4;done; +|'); my $g = IO::File->new('for t in 1 2 3 4;do echo two $t; sleep 4;done; +|'); my $h = IO::File->new('for t in 1 2 3 4;do echo three $t; sleep 4;done +; |'); my (@f,@g,@h); my $done; sub newIO { my ($f,$fr) = @_; Event->io( fd => $f, poll => 're', cb => sub { my $e = $_[0]; my $w = $e->w; my $fd = $w->fd; if ($fd->eof) { $pending--; $w->cancel; return; } my $line = <$fd>; push @{$fr}, $line; }, ); $pending++; } Event->idle( min => 1, max => 2, cb => sub { print "last one: $f[-1]"; print "last two: $g[-1]"; print "last three: $h[-1]"; Event::unloop if ($pending == 0); }, ); newIO($f,\@f); newIO($g,\@g); newIO($h,\@h); Event::loop; print @f; print @g; print @h;

Gives:

last one: one 1
last two: two 1
last three: three 1
last one: one 1
last two: two 1
last three: three 1
last one: one 1
last two: two 1
last three: three 1
last one: one 2
last two: two 2
last three: three 2
last one: one 2
last two: two 2
last three: three 2
last one: one 2
last two: two 2
last three: three 2
last one: one 2
last two: two 2
last three: three 2
last one: one 3
last two: two 3
last three: three 3
last one: one 3
last two: two 3
last three: three 3
last one: one 3
last two: two 3
last three: three 3
last one: one 3
last two: two 3
last three: three 3
last one: one 4
last two: two 4
last three: three 4
last one: one 4
last two: two 4
last three: three 4
last one: one 4
last two: two 4
last three: three 4
last one: one 4
last two: two 4
last three: three 4
last one: one 4
last two: two 4
last three: three 4
one 1
one 2
one 3
one 4
two 1
two 2
two 3
two 4
three 1
three 2
three 3
three 4

In reply to Re: Running Subroutines in Parallel by zengargoyle
in thread Running Subroutines in Parallel by yid

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.