Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: buffering when reading from unnamed pipe with child process

by tybalt89 (Monsignor)
on Nov 19, 2020 at 23:05 UTC ( [id://11123857]=note: print w/replies, xml ) Need Help??


in reply to buffering when reading from unnamed pipe with child process

#!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11123851 use warnings; use IO::Pipe; use IO::Handle; use IO::Select; my $pipe = IO::Pipe->new(); my $pid = fork(); die "failed to fork: $!" unless defined $pid; if ($pid) { $pipe->reader(); my $select = IO::Select->new(); $select->add($pipe); my $line = ''; while (1) { my @ready = $select->can_read(1); foreach my $h (@ready) { # my $line = <$h>; if( sysread $h, $line, 8192, length $line ) { # chomp $line; while( $line =~ s/(.*)\n// ) { print "PARENT sees <$1>\n"; } } else { close $pipe }; } } } else { $pipe->writer(); $pipe->autoflush(1); my $c = 0; while (1) { $c++; print "CHILD writing <$c>\n"; $pipe->print("$c\n") or die; sleep 1 unless $c % 5; # pause after each fifth line } }
CHILD writing <1> CHILD writing <2> CHILD writing <3> CHILD writing <4> CHILD writing <5> PARENT sees <1> PARENT sees <2> PARENT sees <3> PARENT sees <4> PARENT sees <5> CHILD writing <6> CHILD writing <7> CHILD writing <8> CHILD writing <9> CHILD writing <10> PARENT sees <6> PARENT sees <7> PARENT sees <8> PARENT sees <9> PARENT sees <10> CHILD writing <11> CHILD writing <12> CHILD writing <13> CHILD writing <14> CHILD writing <15> PARENT sees <11> PARENT sees <12> PARENT sees <13> PARENT sees <14> PARENT sees <15>

Replies are listed 'Best First'.
Re^2: buffering when reading from unnamed pipe with child process
by Santasha (Novice) on Nov 20, 2020 at 08:16 UTC

    Greatly appreciated, thank you.

      I have found setting autoflush in the parent via $|++; also helps; but using the pipe interface directly it also makes sense to do it this way. My recommendation in addition to the explicit flush is to set autoflush in the parent.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11123857]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (6)
As of 2024-04-24 08:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found