Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Question about IPC::Open2

by GaijinPunch (Pilgrim)
on Nov 14, 2006 at 02:10 UTC ( #583879=perlquestion: print w/replies, xml ) Need Help??

GaijinPunch has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks.
I have an in-house application that will not exit after running. The only way I've been able to have my script continue with the data it's been given is to open it up with IPC::Open2 (I believe this was recommended to me quite some time ago, after posting my specific problems).

The issue now is that no matter how much I sleep before issuing my "quit" command to the writer, I only get about half of what I want. I assume this has something to do w/ the way Open2 forks, but that's just a hunch.

An ugly example:
my $pid = open2(*HIS_OUT, *HIS_IN, "/my/application" ) || die $!; sleep 3; print HIS_IN "quit\n"; while ( <HIS_OUT> ) { chomp; &do_something_to($_); }
Even if I sleep for 15, I still only get about half of the full output. If I don't issue the quit command, I get all the output, but of course, my script is stuck, as it will never leave the HIS_OUT loop.

Replies are listed 'Best First'.
Re: Question about IPC::Open2
by Tanktalus (Canon) on Nov 14, 2006 at 04:02 UTC

    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,

Re: Question about IPC::Open2
by sgifford (Prior) on Nov 14, 2006 at 07:50 UTC
Re: Question about IPC::Open2
by BUU (Prior) on Nov 14, 2006 at 02:26 UTC
    Can you simply check if you've got the full output and then exit out of the loop and manually kill the process?
      No... there's no way to know if it's the end or not... other than leave ample time for it to output.
        So uh, basically you want to open a filehandle and read data from it.. and keep reading.. and keep reading.. and you have no way to tell when to stop? Seems rather unsolvable to me.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://583879]
Approved by GrandFather
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2021-05-16 18:13 GMT
Find Nodes?
    Voting Booth?
    Perl 7 will be out ...

    Results (152 votes). Check out past polls.