in reply to Reading from Many Child Processes using Pipes
Slightly simplified and tweaked...
#!/usr/bin/perl # http://perlmonks.org/?node_id=1146579 use IO::Select; use strict; use warnings; $| = 1; my $children = 1000; my $sel = IO::Select->new; my $parentid = $$; for my $i (1..$children) { if( open my $fh, '-|' ) { $sel->add($fh); } else # child { print "Child Created: $$\n"; for (1..10) { select undef, undef, undef, 2 + rand 0.1; printf "[%d] %5.2f\n", $$ - $parentid, rand 100; } exit; } } while( $sel->count ) { for my $fh ( $sel->can_read ) { if( sysread $fh, my $buf, 4096 ) { print "[parent] $buf"; } else { $sel->remove($fh); } } } print "[parent] all children have exited.\n";
NOTE: check your limit on open files (ulimit -n) to see how many children you can have.
Typical systems may have a limit of only 1024, but this might be tweakable.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Reading from Many Child Processes using Pipes
by sved (Novice) on Oct 31, 2015 at 18:48 UTC | |
by Anonymous Monk on Oct 31, 2015 at 19:22 UTC |