in reply to Capturing STDOUT and/or STDERR from Proc::Background

See Watching long processes through CGI (Aug 02)

In the background-ed process, do the capturing/redirecting of stdout/stderr, say with Try::Tiny

when the background process is finished, you look in the cache to get stdout/stderr

Otherwise look into IPC::Run3 and ?node_id=3989;BIT=Gtk%20IPC;a=zentara Re^6: rsync with Gtk2 progressbar, Re: Responsive GUI without threads,

  • Comment on Re: Capturing STDOUT and/or STDERR from Proc::Background

Replies are listed 'Best First'.
Re^2: Capturing STDOUT and/or STDERR from Proc::Background
by mmartin (Monk) on Aug 09, 2013 at 14:28 UTC
    Hey Anonymous Monk, thanks for the reply!

    Ok, cool I'll check out those links. Didn't think to check the cache, or to even add something to the background script
    that could write out to a file instead of just STDOUT... Wasn't sure if I had mentioned that the background process is also
    a script I wrote, so adding something to do the redirecting/capturing sounds like a plan...!

    Great Idea...! Thanks!


    Thanks Again,
    Matt

      I meant Capture::Tiny, along the lines of

      ... my $session = get_session_id(); my $cache = get_cache_handle(); $cache->set( $session, [ 0, "" ] ); # no data yet my $background = go_background( $session ); ... if( not $background->alive ) { if( my $data = $cache->get( $session ) ){ return show_results( $data ); } } ... sub get_cache_handle { use Cache::FileCache; return Cache::FileCache->new ... }

      ## stuffintocache.pl #!/usr/bin/perl -- use strict; use warnings; use Getopt::Long(); use Capture::Tiny qw/ capture /; Main( @ARGV ); exit( 0 ); sub Main { my %opt; Getopt::Long::GetOptionsFromArray( \@_, \%opt, 'session=s', ... ) my($stdout, $stderr, $exit) = capture { system( $cmd, @args ); }; my $session = get_session_id(); my $cache = get_cache_handle(); $cache->set( $session, [ 1, $stdout, $stderr, $exit ] ); return; }
        Hey Anonymous Monk, thanks for the reply!

        Cool, thanks for the examples, much appreciated...
        I just checked out the CPAN link you posted for that Module and I like that you can also just
        include arbitrary code inside the capture() Function/Method and get STDERR/STDOUT and a
        return code from whatever you put inside the capture function... Pretty cool, thanks!


        Thanks Again for the Reply,
        Matt