in reply to Running Subroutines in Parallel
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
|
|---|