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
    Spot on AnonymousMonk. Thank you for giving me a much needed pointer.

    I'm curious why you used a select in the child code where I used sleep. Is there an advantage of using select over sleep? Or was it just for simulating random sleep for this example?

      From perldoc -f sleep :

      "sleep Causes the script to sleep for (integer) EXPR seconds"

      I wanted fractional second sleeps, which select provides, without having to use Time::HiRes