This is my guess as to what you want...
I worry about data from the four forked processes arriving interleaved
on the pipe, maybe even in the middle of each other's lines :(
This solution caches each fork's response until complete and then
forwards it to program_A in one piece. I wasn't sure on how to do that
using ForkManager.
For testing's sake I combined program_A with the forking program.
#!/usr/bin/perl # http://perlmonks.org/?node_id=1172353 use strict; use warnings; use IO::Select; $| = 1; my @data_files = qw( one two three four five six seven ); my $maxchildren = 4; my %data_for_handles; my $fh_A; if( open $fh_A, '|-' ) { # parent warn "pipe opened\n"; } else { # child print "program_A started\n"; print while <STDIN>; print "program_A ended\n"; exit; } my $sel = IO::Select->new; while( @data_files or $sel->count ) { while( @data_files and $sel->count < $maxchildren ) { my $file = shift @data_files; if( open my $fh, '-|' ) { # parent $sel->add($fh); } else { # child $| = 1; warn "child $file started\n"; print "$file\n"; sleep 1; print "$file\n"; sleep 1; print "$file\n"; exit; } } if( $sel->count > 0 ) { for my $fh ($sel->can_read) { if(0 < sysread $fh, my $buffer, 16 * 1024 ) { $data_for_handles{$fh} .= $buffer; } else { print {$fh_A} delete $data_for_handles{$fh}; $sel->remove($fh); } } } } close $fh_A;
In reply to Re: How to set pipe first and then use the forkmanager?
by tybalt89
in thread How to set pipe first and then use the forkmanager?
by mlin
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |