Beefy Boxes and Bandwidth Generously Provided by pair Networks vroom
Do you know where your variables are?
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

This is a bit simpler than yours, a lot simpler than the CPAN 'solutions', and it works even if the program produces a lot of output:

#! perl -slw use strict; use IPC::Open3; use threads stack_size => 4096; ## Kill 0,pid does appear to work properly, ## It return true even after the executable is dead & gone sub checkPid{ scalar( `tasklist /nh /fi "pid eq $_[0]"` !~ m[INFO]sm ); } sub execute { my( $timeout, @command, ) = @_; my( $err, $in, $out ) = do{ local *FH; \*FH }; my $pid = open3( $in, $out, $err, @command ) or die "@command : $!/$@/$^E"; my( $Tout ) = threads->create( sub{ my( $Terr ) = threads->create( sub{ my @in = <$err>; return \@ +in } ); my @in = <$out>; return \@in, $Terr->join; } ); sleep 1 while checkPid( $pid ) and --$timeout; kill 3, $pid unless $timeout; return $Tout->join, $timeout == 0; } my @cmd = ( 'perl.exe', q[-le"$|++; print($_), warn( $_), Win32::Sleep 250 for 1 .. 12" ] ); my( $out, $err, $timeout ) = execute( 2, @cmd ); print 'Command ', $timeout ? 'timed out' : 'completed'; print "From stdout:[\n @$out ]" if @$out; print "From stderr:[\n @$err ]\n" if @$err; ( $out, $err, $timeout ) = execute( 5, @cmd ); print 'Command ', $timeout ? 'timed out' : 'completed'; print "From stdout:[\n @$out ]" if @$out; print "From stderr:[\n @$err ]" if @$err;

Produces:

C:\test>869942 Command timed out From stdout:[ 1 2 3 4 5 ] From stderr:[ 1 at -e line 1. 2 at -e line 1. 3 at -e line 1. 4 at -e line 1. 5 at -e line 1. ] Command completed From stdout:[ 1 2 3 4 5 6 7 8 9 10 11 12 ] From stderr:[ 1 at -e line 1. 2 at -e line 1. 3 at -e line 1. 4 at -e line 1. 5 at -e line 1. 6 at -e line 1. 7 at -e line 1. 8 at -e line 1. 9 at -e line 1. 10 at -e line 1. 11 at -e line 1. 12 at -e line 1. ]

The discrepancy between the 1 1/4 seconds of output gathered and the 2 second timeout in the first run, is down to pipe buffering.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

In reply to Re: Windows, threads and IPC::Open3 by BrowserUk
in thread Windows, threads and IPC::Open3 by bloonix

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others imbibing at the Monastery: (10)
    As of 2014-04-21 14:11 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      April first is:







      Results (495 votes), past polls