Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: capture periodic updates from external program

by scorpio17 (Canon)
on May 13, 2014 at 14:04 UTC ( [id://1085907]=note: print w/replies, xml ) Need Help??


in reply to capture periodic updates from external program

See this:
Managing a long running server side process using CGI

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; }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2024-03-28 12:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found