in reply to Re^2: To organize pipe right way.
in thread To organize pipe right way.

Sorry, I neglected to set flush on the pipe, which is why you saw the issue. That can be accomplished by explicitly importing IO::Handle and calling autoflush. If I write a complete, self-contained script wrapping date, it might look like:
#!/usr/bin/perl use strict; use warnings; use POSIX qw( mkfifo ); use IO::Handle; my $svitok_truby = 'tmp.pp'; mkfifo( $svitok_truby, 0700 ) || die "Pipe fail; $svitok_truby : $!" +; local $SIG{CHLD} = "IGNORE"; my @args = ('perl','-MIO::Handle','-E',<<'EOC',$svitok_truby); $| = 1; open my $fh, "<", $ARGV[0]; while(read $fh, my($char), 1) { print $char; } EOC my $pid = fork(); die "Fork failed\n" unless defined $pid; if ($pid == 0) { exec @args; } open (my $FIFO, '>', $svitok_truby) || die "can't open $svitok_truby: + $!"; $FIFO->autoflush(1); while(read STDIN, my($char), 1) { print $FIFO $char; } close($FIFO); END { unlink $svitok_truby; }
To modify this from piping into the toy echo program, you would swap @args (including the here-doc, which runs until EOC) to whatever is appropriate for your program, probably (untested):
my @args = ('/usr/bin/mplayer', '-slave', '-input', "file=$svitok_trub +y", $svitok_na_vosproizvedenie_kom[$i]);
It's possible you will still see buffering issues depending on your terminal, because many terminal programs buffer STDIN by default. To test if that is the problem, you can pipe in commands. So, for my toy above,
perl -E'$|=1;print,sleep 1 for 0..9' | perl script.pl
will slowly print 0 - 9 on the screen, one character a second. For your case, you might want to try pausing and unpausing music once per second.

TLDR: use IO::Handle; and $FIFO->autoflush(1); will set the pipe to hot.


#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Replies are listed 'Best First'.
Re^4: To organize pipe right way.
by nikolay (Beadle) on May 06, 2016 at 09:45 UTC

    I added the autoflush ahd its module -- still effect is the same. -- Repetings became necessary up to 7 instead up to 3 w/ the shell version.

    I have no ideas what to do except to find the piece of code i have written long time ago for such a task -- that i remembere worked perfect. But do not know where the code is.

    Here are the last changes:

    use warnings; use strict; use utf8::all; use POSIX qw( mkfifo ); mkfifo( $svitok_truby, 0700 ) || die '&#1054;&#1096;&#1080;&#1073;&#10 +82;&#1072; &#1089;&#1086;&#1079;&#1076;&#1072;&#1085;&#1080;&#1103; & +#1090;&#1088;&#1091;&#1073;&#1099; '.$svitok_truby.' : '.$!; local $SIG{CHLD} = "IGNORE"; # &#1056;&#1086;&#1076;&#1080;&#1090;&#1077;&#1083;&#1100;. if( $nomer_vosproizvodyshchego_protsessu=fork ){ use IO::Handle; open( $FIFO, '>'.$svitok_truby ) || die '&#1053;&#1077; &#1086;&#109 +0;&#1082;&#1088;&#1099;&#1090;&#1100; &#1090;&#1088;&#1091;&#1073;&#1 +091; '.$svitok_truby.' &#1085;&#1072; &#1079;&#1072;&#1087;&#1080;&#1 +089;&#1100; : '.$!; # Same thing? -- I.e. not working? $|=1; $FIFO->autoflush( 1 ); use Term::ReadKey; ReadMode 'cbreak'; while( 1 ){ $knopka=&ReadKey( 0 ); if( $knopka eq ' ' ){ print $FIFO 'pause'; }elsif( $knopka eq '0' ){ print $FIFO 'quit'; last; } } # &#1056;&#1077;&#1073;&#1105;&#1085;&#1086;&#1082;. }else{ for( $i=0; $i<=$#svitok_na_vosproizvedenie; $i++ ){ system( '/usr/bin/mplayer -slave -input file='.$svitok_truby.' '.$ +svitok_na_vosproizvedenie_kom[$i] ); } } ReadMode 'normal'; close( FIFO ); unlink $svitok_truby;

    Thanks for your answers.

      use warnings; use strict;
      Your posted code does not pass strict. This tells me you are not posting the same code you are running. The confusion on this thread is largely driven by not being able to reach mutual understanding of issues. See How do I post a question effectively?. Do not include strict if you don't use it, and don't post code that doesn't compile.
      if( $nomer_vosproizvodyshchego_protsessu=fork ){
      This does not consider the possibility that the fork has returned undef, i.e., has failed. It also means you need to have the rest of your code, none of which is shared, indented into if blocks. That's why I keep using the
      my $pid = fork(); die "Fork failed\n" unless defined $pid; if ($pid == 0) { $| = 1; exec @args; }
      motif.
      # Same thing? -- I.e. not working? $|=1; $FIFO->autoflush( 1 );
      Not the same thing. $| only affects the current active file handle, as controlled by select and by default STDOUT.
      print $FIFO 'pause';
      Are you sure that shouldn't be print $FIFO "pause\n";
      for( $i=0; $i<=$#svitok_na_vosproizvedenie; $i++ ){ system( '/usr/bin/mplayer -slave -input file='.$svitok_truby.' '.$ +svitok_na_vosproizvedenie_kom[$i] ); }
      So you are opening more than one instance of mplayer? I guess this is supposed to be a playlist. Using a blocking call to system to control playing makes sense; the exec solution here would require monitoring the child for an exit status prior to spawning another child, which has an unnecessary number of moving parts. I think your call would be cleaner with interpolating double quotes, but that's cosmetic. system("/usr/bin/mplayer -slave -input file=$svitok_truby $svitok_na_vosproizvedenie_kom[$i]");
      unlink $svitok_truby;
      The reason I wrapped this in an END block was so that it would always execute, even when the script dies. You never want the file lying around afterward.

      Finally, have you tried the example cases I gave you? Have you tried isolating where the lag could be? Rather than worrying about all the other stuff, just try looping over a pause:

      #!/usr/bin/perl use strict; use warnings; use POSIX qw( mkfifo ); use IO::Handle; my $svitok_truby = 'tmp.pp'; mkfifo( $svitok_truby, 0700 ) || die "Pipe fail; $svitok_truby : $!" +; local $SIG{CHLD} = "IGNORE"; my @args = ('/usr/bin/mplayer', '-slave', '-input', "file=$svitok_trub +y", '/tmp/1.flac'); my $pid = fork(); die "Fork failed\n" unless defined $pid; if ($pid == 0) { exec @args; } open (my $FIFO, '>', $svitok_truby) || die "can't open $svitok_truby: + $!"; $FIFO->autoflush(1); for my $i (1 .. 10) { print $FIFO 'pause'; sleep 1; } print $FIFO 'quit'; close($FIFO); END { unlink $svitok_truby; }

      #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

        The code has other parts where variables are declared, that's why the posted code is not compiled w/ strict. Actually, i did not mean you will compile it, but rather a look and give me your feedback on possible reason of such pipe behaviour. So, do not look at the strict, or include in the file before your compilation. I publish code pieces as they are, basicly, because of «use POSIX qw( mkfifo )».

        Seems to me that using «$|=1» is redundant here. By design, «$FIFO» has to do all the stuff OR «$|» w/o using «IO» module.

        Also no need for «\n» in «print $FIFO 'pause'» becuse it works as bad as without it.

        I did run your script and pause did not work a single time!