Hard to tell, but a couple of things hit me about possibilities here. First off, the sleep looks dangerous. That's because /my/application could simply fill up its output buffer, and you'd end up with it suspended until you empty it. So perhaps you want to find the current time, and then loop on HIS_OUT - after three seconds (or whatever), issue the quit, and keep looping. e.g.,

my $pid = open2(*HIS_OUT, *HIS_IN, "/my/application" ) || die $!; my $now = time(); # you may want something more precise, whatever. while (<HIS_OUT>) { chomp; do_something_to($_); if ($now and time - $now > 4) { print HIS_IN "quit\n"; $now = undef; # don't keep doing this. } }
The next thing that hits me is that this sounds like a reasonable use for alarm - set an alarm for 3 seconds, and in that alarm print the quit message, but still go directly into the while loop handling HIS_OUT in your normal codepath.

What really hits me is that you want an individual time-out on HIS_OUT. While select would probably be the low-level way to do this, I've found IO::Select to be much simpler. The concept is that you'll select to read from HIS_OUT with a timeout of, say, one second. If the select times out, then you issue your quit, and go back into the loop - this time it should exit because the filehandle closes from the other end. However, while this may be the right algorithm in C where you don't have things like closures, the alarm methods might be simpler in perl.

Good luck,


In reply to Re: Question about IPC::Open2 by Tanktalus
in thread Question about IPC::Open2 by GaijinPunch

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!
  • 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, details, 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, summary, 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.