http://qs321.pair.com?node_id=1085907


in reply to capture periodic updates from external program

See this:
Managing a long running server side process using CGI
  • Comment on Re: capture periodic updates from external program

Replies are listed 'Best First'.
Re^2: capture periodic updates from external program
by mabossert (Scribe) on May 15, 2014 at 17:32 UTC

    This is getting me all sorts of frustrated. I have reverted to trying a simple test script and external program. The external program does nothing more than print a counter and then sleep for 1 second. I cannot seem to get its output while it is executing...Here is the code for both. Any help would be GREATLY appreciated...

    the "child" program:

    #!/usr/bin/env perl use strict; use warnings; use 5.016; use Carp qw(cluck carp croak); my $c=1; for(1..15) { say 'COUNT: '.$c; $c++; sleep(1); }

    And here is the "parent" program...again, I am open to ANY solution that works. By "works", I mean that I need the output of the child program to be printed as it comes in. For the "real" program, I will need to send a web socket message every time output is received from the child program...the output will be a progress indicator (e.g. 10% complete, 15% complete, etc.).

    First, with Capture::Tiny: (no dice)

    #!/usr/bin/env perl use strict; use warnings; use 5.016; use Carp qw(cluck carp croak); use Data::Dumper; use Capture::Tiny ':all'; local $| = 1; my $cmd = "/tempssd/bossert/bin/testchild.pl &"; my ($stdout, $stderr, @result) = tee { system($cmd); }; while (my $line = <$stdout>) { say 'GOT: '.$line; last if $line =~ m/^FINISHED/; }

    Next, with IPC::Run: (Note, I get the STDOUT AFTER it runs...and it seems that the code ref for the stdout handling never fires.

    #!/usr/bin/env perl use strict; use warnings; use 5.016; use Carp qw(cluck carp croak); use Data::Dumper; use IPC::Run qw(run); local $| = 1; my @cmd = ("/tempssd/bossert/bin/testchild.pl"); run \@cmd, '<', \undef,'>&',\&stdoutH or die "cat: $?"; sub stdoutH { my ($in) = @_; chomp $in; say 'STDOUT: '.$in; }