in reply to IO::Pipe and IO::Select

Seems fine to me, given that I cant see the all important read_line sub

Here's my version, with some important revisions

#!/usr/bin/perl -w use strict; use IO::Select; use IO::Pipe; use IO::Handle; my $debug = 1; my @connect = ("3","2","1"); my %msgs; @msgs{@connect} = qw/hooray hip hip/; my $select_ch = IO::Select->new(); foreach my $msg (@connect) { # for each write, fork a child if ($msg) { # fork and open child for reading my $pipe = IO::Pipe->new(); if (my $pid = fork() ) { #parent my $fh = $pipe->reader(); $debug && print "parent $$ forked $pid\n"; $fh->blocking(0); # set non-blocking I/O $debug && print "parent adding readable filehandle ($fh)\n"; $select_ch->add($fh); } else { #child my $childhandle = $pipe->writer(); my $mt = $msgs{$msg}; sleep $msg; print STDERR "$$ child $msg writing to pipe $msg, response $mt +\n"; print $childhandle "child $msg, response $mt"; exit 0; #kill the child process } } } # done with processing this read # here we read for responses from the filehandles my @readyfiles; #array for select to populate while ( @readyfiles = $select_ch->can_read() ) { foreach my $rh (@readyfiles) { # read from filehandle, send back string print "$$ calling IO::Handle::getline\n"; my $child_resp_string = $rh->getline(); $debug && print "$$ read |$child_resp_string|\n"; # only one read per handle, so close $debug && print "$$ removing @{[ref $rh]}\n"; $select_ch->remove($rh); $rh->close; $debug && print "$$ @{[$select_ch->count()]} handles left to read\ +n"; } } print "exiting...\n"; exit 0;

Platforms
Linux itdevtst 2.4.20-8 #1 Thu Mar 13 17:54:28 EST 2003 i686 i686 i386 GNU/Linux
SunOS robin 5.8 Generic sun4u sparc SUNW,Ultra-60

Perl versions
This is perl, v5.8.2 built for i686-linux
This is perl, v5.8.1 built for sun4-solaris

Output

le99007@robin:~/src/perl> ./pipe.pl parent 7146 forked 7147 parent adding readable filehandle (IO::Pipe::End=GLOB(0x195708)) parent 7146 forked 7148 parent adding readable filehandle (IO::Pipe::End=GLOB(0x1957a4)) parent 7146 forked 7149 parent adding readable filehandle (IO::Pipe::End=GLOB(0x151eb0)) 7149 child 1 writing to pipe 1, response hip 7146 calling IO::Handle::getline 7146 read |child 1, response hip| 7146 removing IO::Pipe::End 7146 2 handles left to read 7148 child 2 writing to pipe 2, response hip 7146 calling IO::Handle::getline 7146 read |child 2, response hip| 7146 removing IO::Pipe::End 7146 1 handles left to read 7147 child 3 writing to pipe 3, response hooray 7146 calling IO::Handle::getline 7146 read |child 3, response hooray| 7146 removing IO::Pipe::End 7146 0 handles left to read exiting...

Same result on the Linux box

+++++++++++++++++
#!/usr/bin/perl
use warnings;use strict;use brain;

Replies are listed 'Best First'.
Re: Re: IO::Pipe and IO::Select
by brinogordon (Novice) on Mar 02, 2004 at 18:00 UTC
    Thanks for the help! Strangely enough, when I copied the code I had posted (which was an edited version of my full test code), it worked correctly. Still not sure why my first version was failing.

    I had avoided $pipe->getline($fh) because the documentation said it behaved like <$fh> and I was under the impression that select (even IO::Select) and <> were incompatible. Now that I think about it, behaving like <> and actually being <> are two different things. So, now I've converted my production code to use IO::Pipe and now have a whole new set of problems :)

    Thanks again for your assitance.