Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

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;


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

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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?

    What's my password?
    Create A New User
    [mz2255]: I'm trying to post a question to "seekers of perl wisdom" but always getting a permission denied when I want to submit. There are no links in my post...
    [ambrus]: again? someone complained about this just a few days ago (although eventually they could post)
    [choroba]: Can you post the question in mz2255's scratchpad?

    How do I use this? | Other CB clients
    Other Users?
    Others musing on the Monastery: (9)
    As of 2017-10-19 15:19 GMT
    Find Nodes?
      Voting Booth?
      My fridge is mostly full of:

      Results (255 votes). Check out past polls.