in reply to Hash Pipe Problem

T'is easier with threads:

#! perl -sw use strict; use threads; use Thread::Queue; $|++; my $Q = new Thread::Queue; async { open my $fh, q[ perl -le"$|++; sleep(1), print qq[$$: $_] for 1 .. 100" | ] or die $!; $Q->enqueue( $_ ) while <$fh>; $Q->enqueue( undef ); }->detach; async { open my $fh, q[ perl -le"$|++; sleep(1), print qq[$$: $_] for 1 .. 100" | ] or die $!; $Q->enqueue( $_ ) while <$fh>; $Q->enqueue( undef ); }->detach; for ( 1 .. 2 ) { print while defined( $_ = $Q->dequeue ); }

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."

Replies are listed 'Best First'.
Re^2: Hash Pipe Problem
by ikegami (Patriarch) on Feb 26, 2009 at 20:23 UTC
    For comparison, here's a version without threads:
    #! perl -sw use strict; use threads; use IO::Select; $|++; my $sel = IO::Select->new(); my %children; for (1..2) { open my $fh, q[ perl -le'$|++; sleep(1), print qq[$$: $_] for 1 .. 10' | ] or die $!; $sel->add($fh); $children{$fh} = ""; } while (%children) { for my $fh ( $sel->can_read() ) { our $buf; local *buf = \( $children{$fh} ); # alias sysread($fh, $buf, 4096, length($buf)) or do { print("$buf\n") if length($buf); $sel->remove($fh); delete($children{$fh}); next; }; print $1 while $buf =~ s/^(.*\n)//; } }

    Definitely more complex and less scalable.

      Hm. I tried your code above--the only change was 's for "s in the command line and...it consumes 100% cpu, never produces any output and I had to ^C to terminate it.

      Like I said, t'is easier with threads.


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
        You very well know why it doesn't work. (select doesn't work on anything but sockets in Windows.) No need for the attitude, especially with someone who agrees with you.